初学VB,做了个加密文件的小程序。请大家帮忙看看加密算法,欢迎指教!!
afasl 2003-01-16 09:56:17 Sub FileLock(InputFile As String, PassWord As String, Locked As Boolean)
Dim Char(0) As Byte
Dim OutChar(0) As Byte
Dim key() As Byte
Dim Check(9) As Byte
Dim X As Integer
Dim Y As Integer
Dim KeyLengh As Integer
Dim FileLengh As Long
Dim Count As Long
Dim F As Variant
Dim Fhand As Variant
Dim Fnum1 As Integer, Fnum2 As Integer
Dim CheckOk As Boolean, OutFile As String
Set F = CreateObject("Scripting.FileSystemObject")
If F.fileexists(InputFile) Then
On Error Resume Next
If GetAttr(InputFile) And vbReadOnly Then
SetAttr InputFile, vbNormal
End If
OutFile = InputFile & ".tmp"
Fnum1 = FreeFile
Open InputFile For Binary As #Fnum1
Get Fnum1, , Check
CheckOk = True
For Y = 0 To 9
If Check(Y) <> (AscB(Mid("Iencrypted!", Y + 1, 1)) + 50) Then
CheckOk = False
End If
Check(Y) = AscB(Mid("Iencrypted!", Y + 1, 1)) + 50
Next Y
If Locked = False And CheckOk = True Then
MsgBox "文件" & InputFile & "已经加密过了。本程序不再加密了!"
Close #Fnum1
Exit Sub
End If
If Locked = True And CheckOk = False Then
MsgBox "文件" & InputFile & "没有加密。本程序无法解密!"
Close #Fnum1
Exit Sub
End If
KeyLengh = Len(PassWord) * 2
FileLengh = FileLen(InputFile)
key = PassWord
Count = 0
'MsgBox FileLengh \ KeyLengh & " " & FileLengh & " " & KeyLengh & " " & FileLengh - (KeyLengh * (FileLengh \ KeyLengh))
If Locked = False And CheckOk = False Then
'加密部分
Close #Fnum1
Fnum1 = FreeFile
Open InputFile For Binary As #Fnum1
Fnum2 = FreeFile
Open OutFile For Binary As #Fnum2
Put Fnum2, , Check
Label4.Caption = "正在加密请稍等.." & vbCrLf & "文件大小:" & vbCrLf & FileLengh & "字节"
ProgressBar1.Visible = True
DoEvents
Do While Not EOF(Fnum1)
For Y = 0 To KeyLengh - 3 Step 2
Count = Seek(Fnum1)
Get Fnum1, , Char
'MsgBox Char(0) & " " & key(Y)
If Char(0) > 0 Then
If (CInt(Char(0)) + CInt(key(Y))) <= 255 Then
OutChar(0) = Char(0) + key(Y)
Else
OutChar(0) = CByte(CInt(Char(0)) + CInt(key(Y)) - 255)
End If
Else
OutChar(0) = Char(0)
End If
'MsgBox OutChar(0)
Put Fnum2, , OutChar
ProgressBar1.Value = Round((Count / FileLengh), 4) * 30000
If Count = FileLengh Then
Exit Do
End If
Next Y
Loop
Close #Fnum2
End If
If Locked = True And CheckOk = True Then
'解密部分
Fnum2 = FreeFile
Open OutFile For Binary As #Fnum2
Label4.Caption = "正在解密请稍等.." & vbCrLf & "文件大小" & FileLengh & "字节"
ProgressBar1.Visible = True
DoEvents
Do While Not EOF(Fnum1)
For Y = 0 To KeyLengh - 3 Step 2
Count = Seek(Fnum1)
Get Fnum1, , Char
'MsgBox Char(0) & " " & key(Y)
If Char(0) > 0 Then
If Char(0) > key(Y) Then
OutChar(0) = Char(0) - key(Y)
Else
OutChar(0) = CByte(255 + CInt(Char(0)) - CInt(key(Y)))
End If
Else
OutChar(0) = Char(0)
End If
'MsgBox OutChar(0)
Put Fnum2, , OutChar
ProgressBar1.Value = Round((Count / FileLengh), 4) * 30000
If Count = FileLengh Then
Exit Do
End If
Next Y
Loop
Close #Fnum2
End If
Close #Fnum1
ProgressBar1.Visible = False
Label4.Caption = "请在上面选择文件 并且输入密匙。"
'加密解密结束恢复文件,并且备份
If F.fileexists(InputFile & ".bak") Then
Set Fhand = F.GetFile(InputFile & ".bak")
Fhand.Delete
End If
Set Fhand = F.GetFile(InputFile)
Fhand.Move (InputFile & ".bak")
SetAttr (InputFile & ".bak"), vbHidden
Set Fhand = F.GetFile(OutFile)
Fhand.Move (InputFile)
MsgBox "搞定了。本程序为了安全在您原文件目录下建立了原文件的备份!" & vbCrLf & _
"如果您觉得不需要或者不安全请您自行删除!" & _
"注意!!!!备份文件为了安全是隐藏属性的!", vbOKOnly + vbExclamation, "注意!!!!"
Else
MsgBox "文件" & InputFile & "不存在!请您重新选择!", vbOKOnly, "文件加密器"
End If
End Sub