Question Simple Encryption

Rayko

New member
Joined
Aug 1, 2008
Messages
1
Programming Experience
Beginner
hi, i am using ascii and octal for a simple encryption function

VB.NET:
    Private Function Encrypt(ByRef Text As String) As String
        Dim encryption As String
        Dim letter As Char
        Dim i As Integer

        For i = 0 To Text.Length - 1
            letter = Text.Substring(i)
            encryption = encryption & Oct(Asc(letter)) + Oct(Asc("K")) & "."
        Next
        Encrypt = encryption
    End Function

However now, i am having trouble writing the decryption function, a sample encrypted message would be like this

"Hello" ---> "110113.145113.154113.154113.157113."
 
Strange

That is a strange way of encrypting something. Basically taking a character, changing it's decimal ASCII value to octal, converting that to a string, then taking the ASCII value of "K", converting it to octal, then the octal to a string, and adding that onto the end of the previous string.

To conver it (and it's pretty messy looking, so I'm sure there's a better way) try:

VB.NET:
    Private Function Decrypt(ByRef Text As String) As String
        Dim Decryption As String = ""
        Dim Number(), cnvrt As String
        Dim i As Integer
        'If the last character is a period, remove it
        If Text.EndsWith(".") Then Text = Text.Remove(Text.Length - 1)
        Number = Split(Text, ".") 'Split the Text into an array
        For Each i In Number
            cnvrt = Strings.Left(Convert.ToString(i), 3)
            Decryption &= Strings.Chr(Convert.ToInt32(cnvrt, 8))
        Next i
        Return Decryption
    End Function


To make the encryption and decryption easier (yet slightly harder to break), change one line in your encryption from
VB.NET:
encryption = encryption & Oct(Asc(letter)) + Oct(Asc("K")) & "."
to
VB.NET:
encryption &= Oct(Asc(letter)=Asc("K")) & "."
That will take the ASCII value of a letter, add the ASCII value of "K", then convert all that to octal.

It also simplifies decryption:
VB.NET:
    Private Function Decrypt(ByRef Text As String) As String
        Dim Decryption As String = ""
        Dim Number(), cnvrt As String
        Dim i As Integer
        'If the last character is a period, remove it
        If Text.EndsWith(".") Then Text = Text.Remove(Text.Length - 1)
        Number = Split(Text, ".") 'Split the Text into an array
        For Each i In Number
            Decryption &= Strings.Chr(Convert.ToInt32(i, 8)-asc("K"))
        Next i
        Return Decryption
    End Function

A couple lines shorter, and easier to deal with.
===============
I have one program that has to write an encrypted file to a location on our server, and that file is read back by another program, which decrypts on the fly.

Because I'm crazy, my encryption/decryption is a little different. It takes the entire string, reverses it, takes the ASCII of each character, converts it to hex, then takes the two characters that make up the hex and reverses them.

In the end I have a long string of letters and numbers. Take each two characters, reverse them, then convert them to decimal, conver that to ASCII, stick that character in a string, and finally reverse the entre string when you're done.

And I managed the encryption/decryption to do it in 3 lines of code each.

I pitty da fool who has to work with my code in the future!
 
Last edited:

Latest posts

Back
Top