RC4模块只能加密两个字节
Option Explicit
Private Const conKey = "E54YBYB5eytn7kVSr6ub56uyAwfrwerf" 'conKey是加密时的密钥,可以自己更改
'========================================================================
'RC4加密函数
'适用于使用UNICODE的双字节ACCESS,测试证明支持中英文混合的文本加密
'
'加密调用方法:=RC4("需加密的字符串")
'解密调用方法:=RC4("加密后的字符串")
'========================================================================
Public Function RC4(strInp 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(conKey) Then j = 1
k(i) = Asc(Mid(conKey, 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(strInp)
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 & ChrW(AscW(Mid(strInp, X, 1)) Xor Y)
Next
RC4 = Outp
End Function
以上代码加密多字节可以,但解密时超过两个字节就出错,请老师指点下应如何解决
[解决办法]
有现成的API为什么不用呢?google一下CryptAcquireContext