初学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
...全文
179 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pigpag 2003-07-25
  • 打赏
  • 举报
回复
你的程序为什么只对Char(0)加密?
还有,如果你逐字处理,就会使破解变得非常简单。

最好使用Rnd()函数。
Randomize <密码>
然后用XOR位运算。

XOR的计算是这样的:比如一个Byte "A",ASCII=65,则
(65)10=(01000001)2
比如,生成的随机数乘以256取整以后得到162,则
162=(10100010)2
然后,进行XOR计算。注意:1 xor 1=0 xor 0=0, 1 xor 0=0 xor 1=1
所以对同一个数进行两次xor就以回到这个数。
01000001
Xor 10100010
----------------
11100011

解密:
11100011
Xor 10100010
----------------
01000001
懂了吧
since1990 2003-07-25
  • 打赏
  • 举报
回复


经典加密算法在VB中的实现(3)- RC4

public sub main()
dim key as string
for i = 1 to 16
randomize
key = key & chr(rnd * 255)
next i
msgbox rc4(rc4("welcome to plindge studio!", key), key)
end sub
public function rc4(inp as string, key as string) as string
dim s(0 to 255) as byte, k(0 to 255) as byte, i as long
dim j as long, temp as byte, y as byte, t as long, x as long
dim outp as string

for i = 0 to 255
s(i) = i
next

j = 1
for i = 0 to 255
if j > len(key) then j = 1
k(i) = asc(mid(key, j, 1))
j = j + 1
next i

j = 0
for i = 0 to 255
j = (j + s(i) + k(i)) mod 256
temp = s(i)
s(i) = s(j)
s(j) = temp
next i

i = 0
j = 0
for x = 1 to len(inp)
i = (i + 1) mod 256
j = (j + s(i)) mod 256
temp = s(i)
s(i) = s(j)
s(j) = temp
t = (s(i) + (s(j) mod 256)) mod 256
y = s(t)

outp = outp & chr(asc(mid(inp, x, 1)) xor y)
next
rc4 = outp
end function




一个简单实用的 vb 加密/解密算法

Function UserCode(password As String) As String
'用户口令加密
Dim il_bit, il_x, il_y, il_z, il_len, i As Long
Dim is_out As String
il_len = Len(password)
il_x = 0
il_y = 0
is_out = ""
For i = 1 To il_len
il_bit = AscW(Mid(password, i, 1)) 'W系列支持unicode

il_y = (il_bit * 13 Mod 256) + il_x
is_out = is_out & ChrW(Fix(il_y)) '取整 int和fix区别: fix修正负数
il_x = il_bit * 13 / 256
Next
is_out = is_out & ChrW(Fix(il_x))

password = is_out
il_len = Len(password)
il_x = 0
il_y = 0
is_out = ""
For i = 1 To il_len
il_bit = AscW(Mid(password, i, 1))
'取前4位值
il_y = il_bit / 16 + 64
is_out = is_out & ChrW(Fix(il_y))
'取后4位值
il_y = (il_bit Mod 16) + 64
is_out = is_out & ChrW(Fix(il_y))
Next
UserCode = is_out
End Function
Function UserDeCode(password As String) As String
'口令解密
Dim is_out As String
Dim il_x, il_y, il_len, i, il_bit As Long

il_len = Len(password)
il_x = 0
il_y = 0
is_out = ""
For i = 1 To il_len Step 2
il_bit = AscW(Mid(password, i, 1))
'取前4位值
il_y = (il_bit - 64) * 16
'取后4位值
'dd = AscW(Mid(password, i + 1, 1)) - 64
il_y = il_y + AscW(Mid(password, i + 1, 1)) - 64
is_out = is_out & ChrW(il_y)
Next

il_x = 0
il_y = 0
password = is_out
is_out = ""

il_len = Len(password)
il_x = AscW(Mid(password, il_len, 1))

For i = (il_len - 1) To 1 Step -1
il_y = il_x * 256 + AscW(Mid(password, i, 1))
il_x = il_y Mod 13
is_out = ChrW(Fix(il_y / 13)) & is_out
Next
UserDeCode = is_out
End Function

since1990 2003-07-25
  • 打赏
  • 举报
回复
http://www.csdn.net/Develop/list_article.asp?author=jlum99

经典加密算法在VB中的实现(4)- DES (jlum99收藏) Visual Basic 2267 2001-6-17

经典加密算法在VB中的实现(3)- RC4 (jlum99收藏) Visual Basic 1638 2001-6-17

经典加密算法在VB中的实现(2)- MD5 (jlum99收藏) Visual Basic 1978 2001-6-17

经典加密算法在VB中的实现(1)- Base64 (jlum99收藏)

liu584 2003-05-19
  • 打赏
  • 举报
回复
xor位运算,我理解为不进位的加法,
用来加密,一般过程是,一个一个的
取出字符的Ascii码,xor上随机数,
然后还原为字符
因为xor的运算是可逆的,所以用xor加密,解密是同一过程。

afasl 2003-01-16
  • 打赏
  • 举报
回复
顺便请教一下!XOR运算的原理?我查了MSDN光盘也没个比较明确的东西。:(

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧