关于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 = LenB(password)
il_x = 0
il_y = 0
is_out = " "
For i = 1 To il_len
il_bit = AscB(MidB(password, i, 1)) 'b系列支持中文
il_y = (il_bit * 13 Mod 256) + il_x
is_out = is_out & ChrB(Fix(il_y)) '取整 int和fix区别: fix修正负数
il_x = il_bit * 13 / 256
Next
is_out = is_out & ChrB(Fix(il_x))
password = is_out
il_len = LenB(password)
il_x = 0
il_y = 0
is_out = " "
For i = 1 To il_len
il_bit = AscB(MidB(password, i, 1))
'取前4位值
il_y = il_bit / 16 + 64
is_out = is_out & ChrB(Fix(il_y))
'取后4位值
il_y = (il_bit Mod 16) + 64
is_out = is_out & ChrB(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 = LenB(password)
il_x = 0
il_y = 0
is_out = " "
For i = 1 To il_len Step 2
il_bit = AscB(MidB(password, i, 1))
'取前4位值
il_y = (il_bit - 64) * 16
'取后4位值
'dd = AscW(Mid(password, i + 1, 1)) - 64
il_y = il_y + AscB(MidB(password, i + 1, 1)) - 64
is_out = is_out & ChrB(il_y)
Next
il_x = 0
il_y = 0
password = is_out
is_out = " "
il_len = LenB(password)
il_x = AscB(MidB(password, il_len, 1))
For i = (il_len - 1) To 1 Step -1
il_y = il_x * 256 + AscB(MidB(password, i, 1))
il_x = il_y Mod 13
is_out = ChrB(Fix(il_y / 13)) & is_out
Next
UserDeCode = is_out
End Function
上面的加密出来都是 "? ",无法解密了
[解决办法]
函数没问题, 你的调用方法有问题:
Private Sub Form_Load()
Dim s As String
s = UserCode( "1234中文 ")
Debug.Print s '这里显示出来的就是一串?
Debug.Print UserDeCode(s) '这里显示出来的是加密前的原文了
End
End Sub
[解决办法]
解密倒没有问题!
Private Sub Form_Load()
Dim c As String
c = UserCode( "我lasdf ")
Debug.Print c
Debug.Print UserDeCode(c)
End Sub
但是:il_y = (il_bit * 13 Mod 256) + il_x
is_out = is_out & ChrB(Fix(il_y)) '取整 int和fix区别: fix修正负数
il_x = il_bit * 13 / 256
这里容易出错!如果(il_bit * 13 Mod 256)按近256,再加上il_x就超过了256,再用chrB就会出现溢出错误!如:c = UserCode( "我们lasdf ")就会出错
[解决办法]
同意自己写加解密
比如可以用一个大数组存放字符编码
随机乱序或自定义规则乱序
通过查表法加密
另外还要一个逆数组用来解密
[解决办法]
先把汉字转换成数字,再加密