だから、私は単純な暗号化プログラムを作っています、これはアルゴリズムです:
- 例:char:a;key:10000
- CONTROL 番号 1000 - 97 = 9903 から ASCII 番号を引く
- 結果を 16 進数に変換してから文字列に変換します 9903 = 26AF
- 16 進数の結果を 2 桁のグループに分割 26, AF
- 分割された 16 進値を 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