Reputation: 2335
So, im making a simple encryption program, this is the algorithm:
The problem is that sometimes(code below), depending on the encryption key, the encryption results come out all worn and the decryption function wont work. I have done tests and i know that the problem lies on the encryption function, but i don't know where. Here's the code:
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
Upvotes: 0
Views: 251
Reputation: 6948
The problem is that sometimes(code below), depending on the encryption key, the encryption results come out all worn and the decryption function wont work.
Your main problem that I can find is that sometimes your passing undisplayable codes to the encrypted string, which then treats it as null, which in turn will make the decryption algorithm fail
You might need to figure out what range of keys will give bad values and filter those keys out or use a byte array instead of a string
Upvotes: 1