一個(gè)使用隨機(jī)數(shù)加密字串的算法
發(fā)表時(shí)間:2024-02-08 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]首先這個(gè)算法沒什么特殊之處,只是怕以后找不到,所以放到了這上面 每個(gè)字節(jié)加密后有6種結(jié)果(占兩個(gè)字節(jié),如果需要大于6種的話,就要多用1個(gè)字節(jié),即占3 個(gè)字節(jié)),也就是說(shuō)如果字串占n個(gè)字節(jié)的話,可能產(chǎn)生的結(jié)果為6的n次方個(gè),這個(gè)算法破解的強(qiáng)度不大,大家可以完善一下:'窗體上一個(gè)...
首先這個(gè)算法沒什么特殊之處,只是怕以后找不到,所以放到了這上面
每個(gè)字節(jié)加密后有6種結(jié)果(占兩個(gè)字節(jié),如果需要大于6種的話,就要多用1個(gè)字節(jié),即占3 個(gè)字節(jié)),也就是說(shuō)如果字串占n個(gè)字節(jié)的話,可能產(chǎn)生的結(jié)果為6的n次方個(gè),這個(gè)算法破解的強(qiáng)度不大,大家可以完善一下:
'窗體上一個(gè)按鈕,兩個(gè)listbox
Option Explicit
Private Sub Command1_Click()
Dim i As Long
Dim s As String
For i = 1 To 100
s = encode("這是一個(gè)測(cè)試 hello world")
List1.AddItem s
s = decode(s)
List2.AddItem s
Next
End Sub
Private 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) '最大產(chǎn)生的隨機(jī)數(shù)只能是5,不能再大了,再大的話,就要多用一個(gè)字節(jié)
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
Private 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