Easy encryption

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2498 Views - Last Post: 04 August 2013 - 12:04 PM Rate Topic: -----

#31 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Easy encryption

Posted 04 August 2013 - 12:04 PM

View Postroadfury, on 28 June 2013 - 08:41 PM, said:

I forgot to point out two thing:
  • I picked two-digit groups of Hex number on purpose: 00 in Hex = 00 in Dec, FF in Hex = 255 in Dec. So you will always be able to map back to an ASCII value. Makes things much simpler since you know everything will always be two-digit groups (as long as you "pad" every group).
  • You will have to decrypt dynamically, based on your key.

One thing I did point out:
  • ASCII characters 0 to 31 are called Control Characters (or Non-Printing Characters). Some of them are going to mess up your encryption.
I think this is causing part (or all) of the encryption issue.

Throw this code into a new Visual Studio console project. Run the program and compare the results with this webpage: ASCII Table
Module Module1

    Sub Main()
		For iLoop As Integer = 0 To 255
			Console.WriteLine(iLoop & ". " & """" & Chr(iLoop) & """")
		Next
		Console.ReadKey()
	End Sub

End Module


You should quickly see which ASCII characters show something, and which ones will cause problems (10 and 13 are the best examples). I haven't been able to think of any way around this, other than to use the hex values instead of the converted ASCII characters (i.e. use 26AF instead of &).


Now on to the decryption function.

Every character will be encoded to one or more two-digit Hex groups (always two-digit groups - pad with a "0" if necessary), based on the key. In our "a" example above, the result of the encoder was "&" (26AF). Your decoder will need to know, that based on a key = 1000, two groups of characters = one decoded character.

In your example above, using a key = 68000, the letter "a" = 01093F in Hex (010963 Dec). Therefore the decoder will need to know that a key = 68000 will require three groups of hex characters to make one decoded character.

This is where the dynamic part will come into play. You will need a function that returns the number of two-digit groupings that make up an ASCII character.

Once you have that function, do the following:
Convert each ASCII character to Decimal value (Asc() function), then Hex value (be sure to pad if necessary)
Use the dynamic function to group all the Hex values

Then for each of those groups, loop through the following:
Convert to decimal
Subtract converted value from your key
Find the ASCII character

Once complete, you should have a decrypted string

You started hinting on this with your code:
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


But it looks like you are locking things in at 4 characters If finalhex.Length Mod 4 = 0 Then. This is where that dynamic function will come into play. Also, make sure you are padding your Hex values (I keep stressing you pad your values because it's critical to the functions).

Keep up the good work! I think you're almost there.

I'm back!
So, i finished the easy part i think, and the encryption function now checks for resulting control characters and outputs the complete hex value if there is a control char(Thanks to Char.IsControl of .NET 4.5). Now comes the tricky part, i changed all the values that the dynamic funtion will interact with, assigning to the variable dynaint (Probably the nicest variable name ever). Also i created a label that appears when the bug is happening, to help on the debugging process. I think all that i need now is the dynamic function itself, i've been cracking my head on it, but i'm blocked on the logic that would work behind it. If anyone has an idea on how to work this out it would be much appreciated (thanks roadfury for the awesome idea).
Code:
Option Strict On
Imports System.Numerics
Public Class MainF

#Region "Fields"

    'tick for random keys
    Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Int32))

#End Region 'Fields

#Region "Methods"

    Public Function GetRandomNumber(Optional ByVal Low As Int32 = 1, Optional ByVal High As Int32 = 100) As Int32
        'generate a randomic number, for the random key
        Return objRandom.Next(Low, High + 1)
    End Function

    Private Function AsciiToChars(ByVal nums As IEnumerable(Of Int32)) As Char()
        'converts ASCII value to char
        Return (From c In nums Select Chr(c)).ToArray
    End Function

    Private Sub De_TextChanged(sender As Object, e As EventArgs) Handles De.TextChanged
        'decrypt
        If (String.IsNullOrEmpty(De.Text)) Then
            DeRes.Text = ""
        Else
            Dim dynaint As Integer = 4
            Dim final As String = ""
            Dim key As Int32
            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 Int32 = 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 dynaint = 0 Then
                Dim newlist As New List(Of String)
                For x As Int32 = 0 To finalhex.Length - 1 Step dynaint
                    newlist.Add(finalhex.Substring(x, dynaint))
                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 = "Invalid Input"
            End If

        End If
    End Sub

    Sub En_TextChanged(sender As Object, e As EventArgs) Handles En.TextChanged
        'encrypt
        If (String.IsNullOrEmpty(En.Text)) Then
            Enres.Text = ""
        Else
            Buglbl.Visible = False
            Dim key As Int32 = CInt(KeyE.Text)
            Dim chars() As Char = En.Text.ToCharArray
            Dim ints(chars.Length) As Int32
            Dim hex(chars.Length) As String
            Dim fex As String = ""
            Dim Mlist As New List(Of String)
            For Loop0 As Int32 = 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 Int32 = 0 To fex.Length - 1 Step 2
                Mlist.Add(fex.Substring(x, 2))
            Next
            Dim fdec(CInt((fex.Length - 2) / 2)) As Int32
            Dim fstr As String
            Dim transtr(fdec.Length) As String
            For y As Int32 = 0 To fdec.Length - 1
                fdec(y) = CInt(Val("&H" & Mlist(y)))
                transtr(y) = Chr(fdec(y))
                If (Char.IsControl(CChar(transtr(y)))) Then
                    Buglbl.Visible = True
                    transtr(y) = Mlist(y)
                End If
            Next
            fstr = String.Join("", transtr)
            Enres.Text = fstr
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'set random key
        Buglbl.Visible = False
        KeyE.Text = CStr(GetRandomNumber(10000, 99999))
        KeyD.Text = KeyE.Text
    End Sub

    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

    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 Region 'Methods

End Class

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3