所以,我做了一个简单的加密程序,算法是这样的:
- 示例:char:a;key:10000
- 从 CONTROL 编号 1000 - 97 = 9903 中减去 ASCII 编号
- 将结果转换为十六进制,然后转换为字符串 9903 = 26AF
- 将十六进制结果分成两位数的组 26, AF
- 将拆分的十六进制值转换为 ASCII 26 = 38 (&) AF = 175 (¯) 你的结果将是:&¯
问题是有时(下面的代码),取决于加密密钥,加密结果全部磨损,解密功能不起作用。我做了测试,我知道问题出在加密功能上,但我不知道在哪里。这是代码:
Option Strict On
Imports System.Numerics
Public Class MainF
'tick for random keys
Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
Private Function AsciiToChars(ByVal nums As IEnumerable(Of Integer)) As Char()
'converts ASCII value to char
Return (From c In nums Select Chr(c)).ToArray
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'set random key
KeyE.Text = CStr(GetRandomNumber(10000, 99999))
KeyD.Text = KeyE.Text
End Sub
Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
'generate a randomic number, for the random key
Return objRandom.Next(Low, High + 1)
End Function
Private Sub GenKey_Click(sender As Object, e As EventArgs) Handles GenKey.Click
'renew key
KeyE.Text = CStr(GetRandomNumber(10000, 99999))
Dim tmptxt As String
tmptxt = En.Text
En.Text = ""
En.Text = tmptxt
KeyD.Text = KeyE.Text
tmptxt = String.Empty
End Sub
Sub En_TextChanged(sender As Object, e As EventArgs) Handles En.TextChanged
'encrypt
If (String.IsNullOrEmpty(En.Text)) Then
Enres.Text = ""
Else
Dim key As Integer = CInt(KeyE.Text)
Dim chars() As Char = En.Text.ToCharArray
Dim ints(chars.Length) As Integer
Dim hex(chars.Length) As String
Dim fex As String = ""
Dim Mlist As New List(Of String)
For Loop0 As Integer = 0 To chars.Length - 1
ints(Loop0) = key - Asc(chars(Loop0))
hex(Loop0) = Conversion.Hex(ints(Loop0))
fex &= hex(Loop0)
Next
If fex.Length Mod 2 <> 0 Then 'Mod returns the remainder of a division calculation. It will be 0 if the number is even.
fex = "0" & fex 'This will change "6AF" to "06AF"
End If
For x As Integer = 0 To fex.Length - 1 Step 2
Mlist.Add(fex.Substring(x, 2))
Next
Dim fdec(CInt((fex.Length - 2) / 2)) As Integer
Dim fstr As String
For y As Integer = 0 To fdec.Length - 1
fdec(y) = CInt(Val("&H" & Mlist(y)))
Next
fstr = AsciiToChars(fdec)
Enres.Text = fstr
End If
End Sub
Private Sub De_TextChanged(sender As Object, e As EventArgs) Handles De.TextChanged
'decrypt
If (String.IsNullOrEmpty(De.Text)) Then
DeRes.Text = ""
Else
Dim final As String = ""
Dim key As Integer
key = CInt(KeyD.Text)
Dim FSTR As String = De.Text
Dim chars() As Char = FSTR.ToCharArray
Dim hexsub(chars.Length) As String
Dim ints(chars.Length) As String
Dim finalhex As String
For loop1 As Integer = 0 To chars.Length - 1
ints(loop1) = CStr(Asc(chars(loop1)))
hexsub(loop1) = Hex(ints(loop1))
Next
finalhex = Join(hexsub, String.Empty)
If finalhex.Length Mod 4 = 0 Then
Dim newlist As New List(Of String)
For x As Integer = 0 To finalhex.Length - 1 Step 4
newlist.Add(finalhex.Substring(x, 4))
Next
Dim sourceNUM(newlist.Count) As Int32
Dim finalascii(newlist.Count) As Int32
Dim finalchar(newlist.Count) As Char
key = CInt(KeyD.Text)
For b As Int32 = 0 To newlist.Count - 1
sourceNUM(b) = Convert.ToInt32(newlist(b), 16)
finalascii(b) = key - sourceNUM(b)
If finalascii(b) >= 32 And finalascii(b) <= 255 Then
finalchar(b) = Chr(finalascii(b))
final &= finalchar(b)
Else : final = "Invalid Input"
End If
Next
DeRes.Text = final
Else
DeRes.Text = ""
End If
End If
End Sub
Private Sub KeyE_TextChanged(sender As Object, e As KeyEventArgs) Handles KeyE.KeyDown
'future idea, enter key will run encription sub
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
End If
End Sub
End Class