有的时候我们使用RC4来编码数据,但产生的字符串已经不是正常的字符串,在存储过程中会造成混乱,以致于无法用RC4来还原原来的数据。解决问题办法就是将这个不正常的字符串进行转换后存储,取出时再转换回来。
两个函数如下:
' 将一个字符数组转换成以逗号分隔的数字字符串 ' 作者:朱亦文 Function String2Num(ByVal s As String) As String If s = "" Then Exit Function Dim i As Integer, l As Integer Dim ByteGB() As Byte ByteGB = s l = UBound(ByteGB) For i = 0 To l String2Num = String2Num & "," & ByteGB(i) Next String2Num = Mid(String2Num, 2) End Function ' 将以逗号分隔的数字字符串还原成一个字符数组 ' 作者:朱亦文 Function Num2String(ByVal s As String) As String If s = "" Then Exit Function Dim aCh, i As Integer, l As Integer aCh = Split(s, ",") l = UBound(aCh) ReDim ByteGB(l) As Byte For i = 0 To l ByteGB(i) = CInt(aCh(i)) Next Num2String = ByteGB End Function
附: 刘小军先生的RC4函数
' conKey是加密时的密钥,可以自己更改 Private Const conKey = "zy123456789" Public Function RC4(strInp As String) As String ' ================================= ' RC4加密函数 ' ================================= ' 适用于使用UNICODE的双字节ACCESS,测试证明支持中英文混合的文本加密 ' ================================= ' 参数: ' strInp 需要加密或解密的字符串 ' ================================= ' ' 这是修改过的RC4加密方法,主要更改是把密钥固定在程序中了 ' 使用方法: ' RC4("345G4")得到"345G4"加密之后的字符串 ' RC4(RC4("345G4"))可以将加密后的字符串还原为"345G4" ' 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