16,749
社区成员
发帖
与我相关
我的任务
分享
Attribute VB_Name = "Module1"
Public Function Encode(ByVal s As String) As String '加密
If Len(s) = 0 Then Exit Function
Dim buff() As Byte
buff = StrConv(s, vbFromUnicode)
Dim i As Long
Dim j As Byte
Dim k As Byte, m As Byte
Dim mstr As String
mstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz" '加密后包含的字符
Dim outs As String
i = UBound(buff) + 1
outs = Space(2 * i)
Dim temps As String
For i = 0 To UBound(buff)
Randomize Time
j = CByte(5 * (Math.Rnd()) + 0) '最大产生的随机数只能是5,不能再大了,再大的话,就要多用一个字节
buff(i) = buff(i) Xor j
k = buff(i) Mod Len(mstr)
m = buff(i) \ Len(mstr)
m = m * 2 ^ 3 + j
temps = Mid(mstr, k + 1, 1) + Mid(mstr, m + 1, 1)
Mid(outs, 2 * i + 1, 2) = temps
Next
Encode = outs
End Function
Public Function Decode(ByVal s As String) As String '解密
On Error GoTo myERR
Dim i As Long
Dim j As Byte
Dim k As Byte
Dim m As Byte
Dim mstr As String
mstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
Dim t1 As String, t2 As String
Dim buff() As Byte
Dim n As Long
n = 0
For i = 1 To Len(s) Step 2
t1 = Mid(s, i, 1)
t2 = Mid(s, i + 1, 1)
k = InStr(1, mstr, t1) - 1
m = InStr(1, mstr, t2) - 1
j = m \ 2 ^ 3
m = m - j * 2 ^ 3
ReDim Preserve buff(n)
buff(n) = j * Len(mstr) + k
buff(n) = buff(n) Xor m
n = n + 1
Next
Decode = StrConv(buff, vbUnicode)
Exit Function
myERR:
Decode = ""
End Function