Easy encryption

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

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

#16 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 11 June 2013 - 06:08 PM

So, i made a few changes to the encryption algorithm, but it turned out in a huge fail. I'm not sure why is it failing, the output repeats itself, rather like this:Posted Image

Why is this happening, does anyone have an idea? I believe it to be something with the aritmetics.

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 CharsToAscii(ByVal chars As IEnumerable(Of Char)) As Integer()
        'converts a char to ASCII value
        Return (From c In chars Select Asc(c)).ToArray

    End Function
    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))
    End Sub
    Function Map(ByVal num As Double, ByVal min1 As Double, ByVal max1 As Double, ByVal min2 As Double, ByVal max2 As Double) As Double
        'maps values, Ex.: from 0 to 10, value is 9.From 0 to 100 value will be 90
        Dim tmpnum As Double = (num - min1) / (max1 - min1)
        Dim resnum As Double = (((tmpnum * (max2 - min2)) + min2))
        Return resnum
    End Function
    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))
    End Sub
    Private Sub En_TextChanged(sender As Object, e As EventArgs) Handles En.TextChanged
        'encrypt
        If (String.IsNullOrEmpty(En.Text)) Then
            Enres.Text = ""
        Else
            Dim charA() As Char = En.Text.ToCharArray
            Dim intA(charA.Length) As Double
            For loop1 As Integer = 0 To charA.Length-1
                intA(loop1) = Asc(charA(loop1))
            Next
            Dim Restring As String = String.Empty
            Dim resint(intA.Length) As Double
            Dim nowkey As Integer = CInt(KeyE.Text)
            For value As Integer = 0 To intA.Length - 1               
                ' resint(value) = CInt(Math.Pow(intA(value), 2))
                resint(value) = intA(value) - CDbl(nowkey)
                If resint(value) = -CDbl(nowkey) Then
                    resint(value) = 256 - CDbl(nowkey)
                End If
            Next
            For i As Int32 = 0 To resint.Length - 1
                resint(i) = Map(resint(i), 0, 255 - CDbl(KeyE.Text), 0, 255)
                Restring &= Convert.ToChar(Convert.ToInt32(resint(i)))
            Next
            Enres.Text = Restring
        End If
    End Sub

    Private Sub De_TextChanged(sender As Object, e As EventArgs) Handles De.TextChanged
        'decrypt-future work
        If (String.IsNullOrEmpty(De.Text)) Then
            DeRes.Text = ""
        Else

        End If
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#17 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Easy encryption

Posted 11 June 2013 - 07:50 PM

Simplify to problem.
I can not decipher the encoding algorithm, from your mess of code.

Describe in words (not code) the algorithm.
Provide some examples of input and the intended output.

I find it a lot easier to tinker and experiment with code in Console Application. (Then move the resultant code into a fully windowed application.

Public Module Module1
 
  Sub  Main()
    Dim someText As String  = "Dream.In.Code"
    Dim codeNum  As Integer = 10000
    Dim encoded = Encode( SomeText , codeNum )
    Dim decoded = Decode( endcoded , codeNum )
    Dim result  = someText = decoded
    Debugger.Break
  End Sub

  Public Function Encode( Text As String, Code As Integer) As String

  End Function

  Public Function Decode( Text As String, Code As Integer) As String

  End Function

End Module


This post has been edited by AdamSpeight2008: 11 June 2013 - 07:51 PM

Was This Post Helpful? 1
  • +
  • -

#18 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 13 June 2013 - 03:49 PM

Ok, i agree, my code looks like crap, its extremely messy. So, i had two ideas of test algorithm, the first one uses mersenne primes, basically it would get the char value, lets say char=a, so a=97 (ASCII), the resulting number would be (97^Code)-1, that would result in a so said mersenne prime. The output would be the equivalente value, Example:char = a, code = 10000. (97^10000)-1, that would be this, then we would find out what this number represents in the 0-255 ASCII scale and print it.
The other algorithm is way simpler, its F(char)= char-code, so code =10000, char = 97(a),F(char)=-9903. The main problem i seem to always find is how to "cast" the algorithm results back into chars. The mapping values ideas seemed good, but now its seems kind of dumb. I think my question would be, rather than how to fix that algorithm (that seems, to me, broen at the very base idea of turning result numbers to chars), how to turn the result numbers back to chars (different than the one we started with of course). I'm not sure i made myself much clear, but suming it up, i believe that my problem isn't on the idea of the algorithm, but converting the resulting output back into a char.
Also, "test rig"
Module Module1
    Sub Main()
        Dim txt As String = "I love D.I.C"
        Dim code As Integer = 10000
        Dim encoded As String = Encode(txt, code)
        Console.WriteLine(encoded)
        Dim decoded As String = Decode(encoded, code)
        Console.WriteLine(decoded)
    End Sub
    Public Function Encode(Text As String, Code As Integer) As String
        
    End Function
    Public Function Decode(Text As String, Code As Integer) As String

    End Function
End Module


This post has been edited by GunnerInc: 14 June 2013 - 08:37 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#19 roadfury  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Easy encryption

Posted 13 June 2013 - 05:03 PM

So to summarize, you want to take 97 (for example), manipulate it with another number like 10000 (for example), and then convert the result back to a number between 0 and 255 (although you really want 32 to 255, since 0 to 31 contain control characters: ASCII Table). Then you want to be able to reverse it with the same manipulation number.

This might work:
1. Subtract ASCII number from CONTROL number
1000 - 97 = 9903

2. Convert result to Hexadecimal and then into a string
9903 = 26AF

3. Split Hex result into groups of two digits
26, AF

4. Convert split Hex values to ASCII
26 = 38 (&)
AF = 175 ()

Your result would be: &

But that's going to give you two or more characters for the price of one. I don't think you'll be able to do a one-to-one conversion with your methodology. Or you need a mathematician. Also, this doesn't handle what to do with ASCII characters below 32.

Note: In step 3, you may have to "buffer" the leading character with a 0 if the length isn't divisible by 2 - Ex. 6AF should be changed to 06AF before splitting.
Was This Post Helpful? 1
  • +
  • -

#20 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 14 June 2013 - 08:24 PM

Ok, i managed to make all that, ill start to work on the decode function now, and also, how would the "buffering" happen? I thought about it but just can't find a way to make it.
Module Module1
    Sub Main()     
        Dim txt As String = "D.I.C is awesome"
        Dim code As Integer = 10000
        Dim encoded As String = Encode(txt, code)
        Console.WriteLine(encoded)
        Dim decoded As String = Decode(encoded, code)
        Console.WriteLine(decoded)
    End Sub
    Public Function Encode(Text As String, Code As Integer) As String
        Dim key As String = GetRandomNumber(10000, 99999)
        key = 10000 'On for testing
        Dim chars() As Char = 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
        For x As Integer = 0 To fex.Length - 1 Step 2
            Mlist.Add(fex.Substring(x, 2))
        Next
        Dim fdec((fex.Length - 2) / 2) As Integer
        Dim fstr As String
        For y As Integer = 0 To fdec.Length - 1
            fdec(y) = Val("&H" & Mlist(y))            
        Next
        fstr = AsciiToChars(fdec)
        Return fstr
    End Function
    Public Function Decode(Text As String, Code As Integer) As String

    End Function
    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
    Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
        'generate a randomic number, for the random key
        Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
        Return objRandom.Next(Low, High + 1)
    End Function    
End Module


This post has been edited by GunnerInc: 14 June 2013 - 08:37 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#21 roadfury  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Easy encryption

Posted 15 June 2013 - 09:43 AM

Maybe "Pad" would be a better term than "Buffer". Basically you're just adding a "0" to the front of your hex string (padding) if the length isn't evenly divisible by 2:
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



That will ensure your Mlist loop will get the first hex value correct. Add the above code just before For x As Integer = 0 To fex.Length - 1 Step 2, and you should be good.
Was This Post Helpful? 1
  • +
  • -

#22 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 16 June 2013 - 11:52 AM

Done. Works really well. Im already working on the decryption.
Module Module1
    Sub Main()     
        Dim txt As String = "D.I.C is awesome"
        Dim code As Integer = 10000
        ' code = GetRandomNumber(10000, 99999)
        Dim encoded As String = Encode(txt, code)
        Console.WriteLine(encoded)
        Dim decoded As String = Decode(encoded, code)
        Console.WriteLine(decoded)
    End Sub
    Public Function Encode(Text As String, Code As Integer) As String
        Dim key As String = Code
        Console.WriteLine(key)
        Dim chars() As Char = 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((fex.Length - 2) / 2) As Integer
        Dim fstr As String
        For y As Integer = 0 To fdec.Length - 1
            fdec(y) = Val("&H" & Mlist(y))            
        Next
        fstr = AsciiToChars(fdec)
        Return fstr
    End Function
    Public Function Decode(Text As String, Code As Integer) As String

    End Function
    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
    Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
        'generate a randomic number, for the random key
        Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
        Return objRandom.Next(Low, High + 1)
    End Function    
End Module

Was This Post Helpful? 0
  • +
  • -

#23 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 24 June 2013 - 08:01 PM

Ok, i finished the decrypt function and it works amazingly well :smile2:(code at the end). The thing is i noticed an awkward bug on the encrypt function, sometimes, some keys display the encryption results with a couple of spaces before it, and then that also makes decription fail. Why does that happen?


Code (Thanks for all the help on it):
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 CharsToAscii(ByVal chars As IEnumerable(Of Char)) As Integer()
        'converts a char to ASCII value
        Return (From c In chars Select Asc(c)).ToArray
    End Function
    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
    Function Map(ByVal num As Double, ByVal min1 As Double, ByVal max1 As Double, ByVal min2 As Double, ByVal max2 As Double) As Double
        'maps values, Ex.: from 0 to 10, value is 9.From 0 to 100 value will be 90
        Dim tmpnum As Double = (num - min1) / (max1 - min1)
        Dim resnum As Double = (((tmpnum * (max2 - min2)) + min2))
        Return resnum
    End Function
    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)/>
                    finalchar(B)/> = Chr(finalascii(B)/>)
                    final &= finalchar(B)/>
                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

Was This Post Helpful? 0
  • +
  • -

#24 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 26 June 2013 - 01:00 PM

Ok, i just tried to find some kind of key limit to when the keys start bugging but i couldnt, some messages bug with that key, others dont. Does anyone know why is this happening? i think it has something to do with arrays, since most of the times it only codes to the fifth letter of the sentence.
Was This Post Helpful? 0
  • +
  • -

#25 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 27 June 2013 - 06:13 PM

Ok, i believe i found the source of my bug!
It lies on the encryption function, when the encryption key (control number) generates a 6 character long hex string, instead of a 4 character long one!
I'm pretty sure thats it, can anyone help me confirm it?
Test: Key:68000;Char:a(97)
fex:"01093F"
Mlist: (0) "01" String
(1) "09" String
(2) "3F" String

fdec: (0) 1 Integer
(1) 9 Integer
(2) 63 Integer

Result string: ?

This post has been edited by Raminator: 27 June 2013 - 06:16 PM

Was This Post Helpful? 0
  • +
  • -

#26 roadfury  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Easy encryption

Posted 28 June 2013 - 01:41 PM

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.

This post has been edited by roadfury: 28 June 2013 - 05:07 PM

Was This Post Helpful? 1
  • +
  • -

#27 AvidDismantler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 30-June 13

Re: Easy encryption

Posted 30 June 2013 - 08:19 PM

I was going to suggest:

            Dim charA() As Char = en.Text.ToCharArray
            Dim intA() As Integer = encode.CharsToAscii(charA)
            Dim Restring As String = ""
            Dim resint(intA.Length - 1) As Integer

            For value As Integer = 0 To intA.Length - 1
                resint(value) = intA(value) - 1
                If resint(value) = -1 Then
                    resint(value) = 255
                End If
            Next

            Restring = encode.AsciiToChars(resint)
            enres.Text = Restring



Basically the same... a little more succinct, making use of your AsciiToChars function.

I was also going to be considerably less rude than the previous respondent. Hope I've succeeded ;)/>

Whoa, disregard that last post. Looks like I missed a lot of posts that came after what I thought was the end of the thread. So it's not really relevant to the present phase of the discussion./
Was This Post Helpful? 1
  • +
  • -

#28 b mitsai  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 09-November 12

Re: Easy encryption

Posted 04 July 2013 - 08:32 PM

dim varByte As Byte()
varByte = System.Text.ASCIIEncoding.ASCII.GetBytes(string)

Was This Post Helpful? 0
  • +
  • -

#29 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 08 July 2013 - 06:10 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.

Firstly, i'd like to apologise for the late answer, it was test week as school.
I really liked your ideas, I'll start to work on them. As a temporary fix I'll look into the key threshold where the bug starts, then I'll try to code your solution. Another idea i had in the meantime was that if the result is a control char, just display the HEX code, but i have no idea how the decryiption would wok around that.
Was This Post Helpful? 0
  • +
  • -

#30 Raminator  Icon User is offline

  • D.I.C Regular

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

Re: Easy encryption

Posted 08 July 2013 - 06:22 PM

View PostAvidDismantler, on 01 July 2013 - 03:19 AM, said:

I was going to suggest:

            Dim charA() As Char = en.Text.ToCharArray
            Dim intA() As Integer = encode.CharsToAscii(charA)
            Dim Restring As String = ""
            Dim resint(intA.Length - 1) As Integer

            For value As Integer = 0 To intA.Length - 1
                resint(value) = intA(value) - 1
                If resint(value) = -1 Then
                    resint(value) = 255
                End If
            Next

            Restring = encode.AsciiToChars(resint)
            enres.Text = Restring



Basically the same... a little more succinct, making use of your AsciiToChars function.

I was also going to be considerably less rude than the previous respondent. Hope I've succeeded ;)/>/>

Whoa, disregard that last post. Looks like I missed a lot of posts that came after what I thought was the end of the thread. So it's not really relevant to the present phase of the discussion./

It's fine, if you have any more tips on how to make the code slimer i would gladly have them. thanks.

View Postb mitsai, on 05 July 2013 - 03:32 AM, said:

dim varByte As Byte()
varByte = System.Text.ASCIIEncoding.ASCII.GetBytes(string)

Sorry, but what is that supposed to do?
Was This Post Helpful? 0
  • +
  • -

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