Public Function r查表(ByVal Code As String) As String
Me.InitTable() '奇、偶、混合表初始化
Me.KeyStr = Code '待解密
Me.KeyB = "" '结果两进制串
Me.InitTable() '初始表 奇、偶、混合表
For i As Integer = 1 To KeyStr.Length / 2 + 1
If i > KeyStr.Length / 2 Then
'加结束符 &H100
Me.rFindOddEvenTable(CInt("&H100"))
Else
'逆KEY
Me.rFindOddEvenTable(CInt("&H" & KeyStr.Substring(i * 2 - 2, 2)))
End If
Next
Dim Str结果 As String = "6B796F21" ''前缀,与算法无关,CM中用
Do While KeyB.Length Mod 8 <> 0 '补足8位
KeyB &= "0"
Loop
'KeyB 是rFindOddEvenTable中的结果
' 每8位二进制 倒序 就是 结果了
For i As Integer = 0 To Me.KeyB.Length \ 8 - 1
Dim b As String = KeyB.Substring(8 * i, 8)
b = Me.Reverse(b)
Str结果 &= BIN_to_HEX(b) '二进制转16进制
Next
Return Str结果
End Function
Sub InitTable()
Dim i As Integer = 1
TableDbl(0) = 0
TableEven(0) = 0
TableOdd(0) = 0
Do
TableDbl(i) = (i - 1) \ 2
i += 1
Loop While i <= &H200
i = 0
Do
TableEven(i) = 2 * i + 2
TableOdd(i) = 2 * i + 1
i += 1
Loop While i <= &HFF
End Sub
''解查表
Function rFindOddEvenTable(ByVal Findvalue As Integer) As Boolean
'Dim value As Long = 0
Dim t As Integer = Findvalue
Findvalue += &H100
Dim KeyA As String = ""
Do
Dim find As Boolean = False
If Findvalue = 0 Then Exit Do
For i As Integer = 0 To Me.TableEven.GetLength(0) - 1 '查偶表
If Me.TableEven(i) = Findvalue Then
find = True
Findvalue = i
'value = value * 2 + 1
KeyA = KeyA & "1"
Exit For
End If
Next
If Findvalue = 0 Then Exit Do
For i As Integer = 0 To Me.TableOdd.GetLength(0) - 1 '查奇表
If Me.TableOdd(i) = Findvalue Then
find = True
Findvalue = i
'value = value * 2
KeyA = KeyA & "0"
Exit For
End If
Next
If find = False Then
Return False
End If
Loop
KeyB &= Me.Reverse(KeyA) '倒
Me.Odd_Even_Calc(t) '奇、偶、混合表处理
Return True 'Return value <= &HFF
End Function
'字符串倒序
Function Reverse(ByVal original As String) As String
Dim arr As Char() = original.ToCharArray
Array.Reverse(arr)
Return New String(arr)
End Function
'奇、偶、混合表处理
Sub Odd_Even_Calc(ByVal value As Integer)
value += 256
Dim result As Integer = 0
Dim v4 As Integer = 0
Dim v3 As Integer = 0
Do
result = TableDbl(value)
If result = 0 Then Exit Do
v4 = TableDbl(result)
v3 = TableOdd(v4)
If result = v3 Then
v3 = TableEven(v4)
TableEven(v4) = value
Else
TableOdd(v4) = value
End If
If value = TableOdd(result) Then
TableOdd(result) = v3
Else
TableEven(result) = v3
End If
TableDbl(value) = v4
TableDbl(v3) = result
value = v4
Loop While value <> 0
End Sub