VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0"; "COMCTL32.OCX"
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "文件加密/解密"
ClientHeight = 4716
ClientLeft = 36
ClientTop = 324
ClientWidth = 7140
Icon = "Jm.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 4716
ScaleWidth = 7140
StartUpPosition = 2 'CenterScreen
Begin ComctlLib.ProgressBar ProgressBar1
Height = 312
Left = 960
TabIndex = 11
Top = 2220
Visible = 0 'False
Width = 5100
_ExtentX = 8996
_ExtentY = 550
_Version = 327682
BorderStyle = 1
Appearance = 1
End
Begin VB.CheckBox CB_Del
Caption = "加密/解密后删除源文件"
Height = 312
Left = 1560
TabIndex = 10
Top = 3180
Width = 2232
End
Begin VB.TextBox T_ConfirmEnCode
Height = 336
IMEMode = 3 'DISABLE
Left = 1560
PasswordChar = "*"
TabIndex = 5
Top = 2580
Width = 1872
End
Begin MSComDlg.CommonDialog CommonDialog1
Left = 420
Top = 3060
_ExtentX = 677
_ExtentY = 677
_Version = 327680
End
Begin VB.TextBox T_EnCode
Height = 336
IMEMode = 3 'DISABLE
Left = 1560
PasswordChar = "*"
TabIndex = 4
Top = 1860
Width = 1872
End
Begin VB.TextBox T_TargetFile
Height = 336
Left = 1560
TabIndex = 3
ToolTipText = "双击选择<目标>文件名"
Top = 1140
Width = 5112
End
Begin VB.TextBox T_SourceFile
Height = 336
Left = 1560
TabIndex = 2
ToolTipText = "双击选择<源>文件名"
Top = 420
Width = 5112
End
Begin VB.CommandButton C_Exit
Cancel = -1 'True
Caption = "退出(&E)"
BeginProperty Font
Name = "隶书"
Size = 16.2
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 552
Left = 4380
TabIndex = 1
Top = 3780
Width = 1632
End
Begin VB.CommandButton C_Ok
Caption = "确定(&O)"
Default = -1 'True
BeginProperty Font
Name = "隶书"
Size = 16.2
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 552
Left = 1080
TabIndex = 0
Top = 3780
Width = 1632
End
Begin VB.Label L_SourceFile
Alignment = 1 'Right Justify
Caption = "源文件名:"
Height = 252
Left = 420
TabIndex = 9
Top = 420
Width = 1092
End
Begin VB.Label L_TargetFile
Alignment = 1 'Right Justify
Caption = "目标文件名:"
Height = 252
Left = 420
TabIndex = 8
Top = 1140
Width = 1092
End
Begin VB.Label L_EnCode
Alignment = 1 'Right Justify
Caption = "密 钥:"
Height = 252
Left = 420
TabIndex = 7
Top = 1860
Width = 1092
End
Begin VB.Label L_ConfirmEnCode
Alignment = 1 'Right Justify
Caption = "确认密钥:"
Height = 252
Left = 420
TabIndex = 6
Top = 2580
Width = 1092
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'从API函数拷贝Windows API用结构及声明,
'用FindFirstFileA()和DeleteFile()判断文件是否存在和删除文件
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 256
cAlternate As String * 14
End Type
Private Declare Function FindFirstFileA Lib "kernel32" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0"; "COMCTL32.OCX"
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "文件加密/解密"
ClientHeight = 4716
ClientLeft = 36
ClientTop = 324
ClientWidth = 7140
Icon = "Jm.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 4716
ScaleWidth = 7140
StartUpPosition = 2 'CenterScreen
Begin ComctlLib.ProgressBar ProgressBar1
Height = 312
Left = 960
TabIndex = 11
Top = 2220
Visible = 0 'False
Width = 5100
_ExtentX = 8996
_ExtentY = 550
_Version = 327682
BorderStyle = 1
Appearance = 1
End
Begin VB.CheckBox CB_Del
Caption = "加密/解密后删除源文件"
Height = 312
Left = 1560
TabIndex = 10
Top = 3180
Width = 2232
End
Begin VB.TextBox T_ConfirmEnCode
Height = 336
IMEMode = 3 'DISABLE
Left = 1560
PasswordChar = "*"
TabIndex = 5
Top = 2580
Width = 1872
End
Begin MSComDlg.CommonDialog CommonDialog1
Left = 420
Top = 3060
_ExtentX = 677
_ExtentY = 677
_Version = 327680
End
Begin VB.TextBox T_EnCode
Height = 336
IMEMode = 3 'DISABLE
Left = 1560
PasswordChar = "*"
TabIndex = 4
Top = 1860
Width = 1872
End
Begin VB.TextBox T_TargetFile
Height = 336
Left = 1560
TabIndex = 3
ToolTipText = "双击选择<目标>文件名"
Top = 1140
Width = 5112
End
Begin VB.TextBox T_SourceFile
Height = 336
Left = 1560
TabIndex = 2
ToolTipText = "双击选择<源>文件名"
Top = 420
Width = 5112
End
Begin VB.CommandButton C_Exit
Cancel = -1 'True
Caption = "退出(&E)"
BeginProperty Font
Name = "隶书"
Size = 16.2
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 552
Left = 4380
TabIndex = 1
Top = 3780
Width = 1632
End
Begin VB.CommandButton C_Ok
Caption = "确定(&O)"
Default = -1 'True
BeginProperty Font
Name = "隶书"
Size = 16.2
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 552
Left = 1080
TabIndex = 0
Top = 3780
Width = 1632
End
Begin VB.Label L_SourceFile
Alignment = 1 'Right Justify
Caption = "源文件名:"
Height = 252
Left = 420
TabIndex = 9
Top = 420
Width = 1092
End
Begin VB.Label L_TargetFile
Alignment = 1 'Right Justify
Caption = "目标文件名:"
Height = 252
Left = 420
TabIndex = 8
Top = 1140
Width = 1092
End
Begin VB.Label L_EnCode
Alignment = 1 'Right Justify
Caption = "密 钥:"
Height = 252
Left = 420
TabIndex = 7
Top = 1860
Width = 1092
End
Begin VB.Label L_ConfirmEnCode
Alignment = 1 'Right Justify
Caption = "确认密钥:"
Height = 252
Left = 420
TabIndex = 6
Top = 2580
Width = 1092
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'从API函数拷贝Windows API用结构及声明,
'用FindFirstFileA()和DeleteFile()判断文件是否存在和删除文件
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 256
cAlternate As String * 14
End Type
Private Declare Function FindFirstFileA Lib "kernel32" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Unload Me
End SubPrivate Sub C_Ok_Click()
If T_SourceFile.Text = "" Then
Beep
MsgBox "必须有源文件名!", vbQuestion, "注意:"
T_SourceFile.SetFocus
Exit Sub
End If
If T_TargetFile.Text = "" Then
Beep
MsgBox "必须有目标文件名!", vbQuestion, "注意:"
T_TargetFile.SetFocus
Exit Sub
End If
If T_SourceFile.Text = T_TargetFile.Text Then '源文件与目标文件相同
Beep
MsgBox "源文件[路径和文件名]不能与目标文件[路径和文件]名相同!", vbOKOnly, "注意!"
T_SourceFile.SetFocus
Exit Sub
End If
If T_EnCode.Text <> T_ConfirmEnCode.Text Then
Beep
MsgBox "确认密钥不对!", vbQuestion, "注意:"
T_EnCode.SetFocus
Exit Sub
End If
Dim FileData As WIN32_FIND_DATA
If FindFirstFileA(T_SourceFile.Text, FileData) = -1 Then '打开源源文件失败
Beep
MsgBox "源文件名不存在!", vbOKOnly, "注意:"
T_SourceFile.SetFocus
Exit Sub
End If
Dim Length As Integer '密钥的长度
Dim I As Long
Dim EnCode As Long '密钥处理成长整型数
Length = Len(T_EnCode)
If Length = 0 Then '密钥为空,则使EnCode为一固定值-30
EnCode = -30
Else
For I = 1 To Length '密钥作相应变换,转换成长整型数
EnCode = EnCode + Asc(Mid(T_EnCode, I, 1)) * 256 * I
Next I
EnCode = -EnCode '密钥取反,做为随机数种子
End If
Dim Fn1 As Long
Dim Fn2 As Long
Fn1 = FreeFile
Open T_SourceFile.Text For Binary Access Read As #Fn1 '打开源文件
Fn2 = FreeFile
Open T_TargetFile.Text For Binary Access Write As #Fn2 '打开目标文件
Form1.MousePointer = 11
ProgressBar1.Visible = True '使进度条可见
Dim Flength As Long '文件的长度
Dim Size As Long '一次处理的数据块长度
Dim ByteBlock() As Byte '不定长数组用来存放和加工读取的数据块
Dim Position As Long '文件指针位置
Dim J As Long
Flength = LOF(Fn1)
Size = 32768 '一次处理32K字节
Position = 0
Rnd (EnCode) '用种子产生随机数
Do While Position < Flength
If Flength - Position < Size Then Size = Flength - Position '不够Size指定长度,则按实际长度处理
ReDim ByteBlock(1 To Size) '重新确定数组的边界
Get #Fn1, Position + 1, ByteBlock '从源文件读取数据到数组 For J = 1 To Size '产生的随机数乘256后与字节进行异或处理
ByteBlock(J) = ByteBlock(J) Xor Int(Rnd * 256)
Next J Put #Fn2, Position + 1, ByteBlock '处理后的数组写入目标文件
Position = Position + Size '移动文件指针位置
ProgressBar1.Value = Int(Position / Flength * 100) '处理进度条
Loop
Close Fn1, Fn2
Beep
If CB_Del Then DeleteFile (T_SourceFile) '删除操作
ProgressBar1.Visible = False '使进度条不可见
Form1.MousePointer = 0
MsgBox "加密/解密完毕!", vbOKOnly, "注意:"
End SubPrivate Sub Form_Load()End SubPrivate Sub T_SourceFile_DblClick()
CommonDialog1.DialogTitle = "打开源文件名:"
CommonDialog1.Filter = "所有文件(*.*)|*.*|*.JM文件|*.JM"
CommonDialog1.ShowOpen
If CommonDialog1.filename <> "" Then
Dim Length As Integer '文件名长度
Dim ExName As String '文件扩展名
Length = Len(CommonDialog1.filename)
ExName = Mid(CommonDialog1.filename, Length - 3 + 1)
T_SourceFile.Text = CommonDialog1.filename
If UCase(ExName) = ".JM" Then '加密后的文件扩展名为.JM
T_TargetFile.Text = Mid(CommonDialog1.filename, 1, Length - 3)
Else
T_TargetFile.Text = CommonDialog1.filename + ".JM"
End If
End If
End SubPrivate Sub T_TargetFile_DblClick()
CommonDialog1.DialogTitle = "打开目标文件名:"
CommonDialog1.Filter = "所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
If CommonDialog1.filename <> "" Then T_TargetFile.Text = CommonDialog1.filename + ".JM"
End Sub
源程序如下:Public Function StringEnDeCodecn(strSource As String, MA) As String
'该函数只对中西文起到加密作用
'参数为:源文件,密码
On Error GoTo ErrEnDeCode
Dim X As Single
Dim CHARNUM As Long, RANDOMINTEGER As Integer
Dim SINGLECHAR As String * 1
Dim strTmp As String
If MA < 0 Then
MA = MA * (-1)
End If
X = Rnd(-MA)
For i = 1 To Len(strSource) Step 1 '取单字节内容
SINGLECHAR = Mid(strSource, i, 1)
CHARNUM = Asc(SINGLECHAR)
g: RANDOMINTEGER = Int(127 * Rnd)
If RANDOMINTEGER < 30 Or RANDOMINTEGER > 100 Then GoTo g
CHARNUM = CHARNUM Xor RANDOMINTEGER
strTmp = strTmp & Chr(CHARNUM)
Next i
StringEnDeCodecn = strTmp
Exit Function
ErrEnDeCode:
StringEnDeCodecn = ""
MsgBox Err.Number & "\" & Err.Description
End Function使用方法:
tmp=stringEnDecn("中华人民共和国",75)
如果要解密的话,只须键入以下语句:
tmp1=stringendecn(tmp,75)
如果要解密的话,只须键入以下语句:
tmp1=StringEnDeCodecn(tmp,75)