locked
Text Encryption and Decryption

    Question

  • Hello, this is my first post on the forum. I've been using VB Express 2010 for a month now and have only completed few tutorials and learned beginners stuff.

     - Window examples are named "Form1" and bold text is used so you can see it clearly... - 

    How can i make text, that is wrote in the first text box - starting from the top of the windows (named "TextBox1") - encrypted* and shown in the second text box (named "TextBox2")?

    *Note that i would only like my own encryption for each letter and symbol that i have on keyboard, like in the examples below (Replacement defined where "a" is "K.3!s9", "m" is "o90#\", "t" is "pJk23;", "e" is "O54gł9b:1", "u" is "rE", "r" is"r", "A" is "cbs7" and "M" is "43L"):

    So someone writes text in the first box and click "Encrypt" button (named "Button1"). The encrypted text is show in the second box as a result.

    After that, make "Decrypt" button (named "Button2") do opposite thing (enter text in the second box and show it decrypted in the first).



    • Edited by Savicava Thursday, March 29, 2012 8:24 PM Unneeded space between letters.
    • Changed type Savicava Friday, March 30, 2012 11:41 AM Suggestion from Cor Ligthert's reply.
    Thursday, March 29, 2012 8:23 PM

Answers

  • Hi,

    first I have to appologize - I posted C# code instead of VB code.

    TextBox1 and TextBox1 are TextBox instances. But you do not want to chenge the whole TextBox. All you want to work with is the text inside the Textboxes. So you have to tell the system, that you want the Text of the Textbox:
    TextBox2.Text = TextBox1.Text
    TextBox2.Text = Replace(TextBox2.Text, encrypta, "1")
    TextBox2.Text = Replace(TextBox2.Text, encryptb, "2")
    TextBox2.Text = Replace(TextBox2.Text, encryptc, "3")

    With kind regards,

    Konrad

    Thanks, that helped me to finalize my program so it can work like in the pictures show below (which was the original question):


    • Edited by Savicava Friday, March 30, 2012 3:53 PM Adding picture with code
    • Marked as answer by Savicava Friday, March 30, 2012 4:57 PM
    Friday, March 30, 2012 3:32 PM

All replies

  • Hello Savicava.

    If you are programming, you have to follow rules like those are created. If you don't do that, you get worse results. 

    In these Microsoft forums you can ask questions and you can start discussions. (Ask questions is default)

    However, if you change ask question to discussion, you are confusing. There is nothing to discuss. 

    The same is with programming, so change in your original message discussion back to question then probable somebody will give you quick an answer and your message is not left alone like currently 11 hours.


    Success
    Cor

    Friday, March 30, 2012 7:25 AM
  • Hi,

    first of all I would recommend a better encryption method. The encryption menthod you described is really weak and quite easy to break.

    And the algorithms are really easy to write:
    a) Encryption: You simply read character after character and for each character you append the encrypted text to a StringBuilder. So it is something like
    var sb = new StringBuilder;
    foreach (var character in myString.ToCharArray())
    {
      switch (character)
      {
        case 'a':
            sb.Append("K3!s9");
            break;
        // .....
      }
    }

    b) The decrption is also quite easy - you could use String.StartsWith(myString.SubString(x), ....) to check if the next character is the character you are checking.... Where x is the number of characters you already decrypted.

    So check out the String class.

    And because you want to learn how to write programs: Optimize it - remove the switch / lot of if statements and introduce your own encryption class. This could hold a translation table (Maybe you want to look at 2 dimensional arrays or maybe you want to use Dictionaries or ... Think about it a little!) and then have generic methods. So a universal approach could be:
    loop through the array and checks if the next step to decrypt is the current entry of the array. (So you could have just one method to decrypt / encrypt think about that point once you got a working solution, too!)

    With kind regards,

    Konrad

    Friday, March 30, 2012 7:48 AM
  • Hi,

    first of all I would recommend a better encryption method. The encryption menthod you described is really weak and quite easy to break.

    And the algorithms are really easy to write:
    a) Encryption: You simply read character after character and for each character you append the encrypted text to a StringBuilder. So it is something like
    var sb = new StringBuilder;
    foreach (var character in myString.ToCharArray())
    {
      switch (character)
      {
        case 'a':
            sb.Append("K3!s9");
            break;
        // .....
      }
    }

    b) The decrption is also quite easy - you could use String.StartsWith(myString.SubString(x), ....) to check if the next character is the character you are checking.... Where x is the number of characters you already decrypted.

    So check out the String class.

    And because you want to learn how to write programs: Optimize it - remove the switch / lot of if statements and introduce your own encryption class. This could hold a translation table (Maybe you want to look at 2 dimensional arrays or maybe you want to use Dictionaries or ... Think about it a little!) and then have generic methods. So a universal approach could be:
    loop through the array and checks if the next step to decrypt is the current entry of the array. (So you could have just one method to decrypt / encrypt think about that point once you got a working solution, too!)

    With kind regards,

    Konrad

    Thank you for quick response, but it is too difficult for me to understand. 

    Code is shown below for the same program i am trying to copy from the video (http://www.youtube.com/watch?v=ZwNAopUrebo&feature=relmfu) I found:

    Error is not present in Visual Basic 6.0, where this program works. So how can I make this work (if I can) in VB Express 2010?

    Friday, March 30, 2012 1:27 PM
  • Hi,

    first I have to appologize - I posted C# code instead of VB code.

    TextBox1 and TextBox1 are TextBox instances. But you do not want to chenge the whole TextBox. All you want to work with is the text inside the Textboxes. So you have to tell the system, that you want the Text of the Textbox:
    TextBox2.Text = TextBox1.Text
    TextBox2.Text = Replace(TextBox2.Text, encrypta, "1")
    TextBox2.Text = Replace(TextBox2.Text, encryptb, "2")
    TextBox2.Text = Replace(TextBox2.Text, encryptc, "3")

    With kind regards,

    Konrad

    Friday, March 30, 2012 1:40 PM
  • I decided to write another replay to show you, what extensions I meant.

    At the moment you just have some variables encrypta, encryptb, encryptc and some hardcoded "1", "2", "3" ... you have 2 times almost the same code ... so it is not that nice ...

    So first we want to store the encryption "key" - something that must be known to encrypt or decrypt something. I mentioned that it could be an Array:

    Dim EncryptionKey(,) As String = New String(2, 1) {{"a", "1"}, {"b", "2"}, {"c", "3"}}

    So we got an Array now which contains pairs ...

    Now we can write a function that translates a given text - and which takes a parameter that tells it if we want to decrypt or encrypt:
        Function Translate(ByRef text As String, ByRef Encrypt As Boolean)
            'Define if we encrypt or decrypt
            Dim source As Integer
            Dim target As Integer
            If (Encrypt) Then
                source = 0
                target = 1
            Else
                source = 1
                target = 0
            End If

            ' Do the replacements
            Dim result As String = text
            For i = 0 To 2
                result = Replace(result, EncryptionKey(i, source), EncryptionKey(i, target))
            Next

            'return the result
            Return result
        End Function

    So when we encrypt, we look for the first item in the array (which is number 0) and replace it with the second (which is the number 1). If we decrypt, it is the other way round.

    And then we simply loop through all pairs (we have 3 pairs ... numbered from 0 to 2) and do the replace.

    I hope this is shows a little bit, what I meant.

    With kind regards,

    Konrad

    Friday, March 30, 2012 1:59 PM
  • Hi,

    first I have to appologize - I posted C# code instead of VB code.

    TextBox1 and TextBox1 are TextBox instances. But you do not want to chenge the whole TextBox. All you want to work with is the text inside the Textboxes. So you have to tell the system, that you want the Text of the Textbox:
    TextBox2.Text = TextBox1.Text
    TextBox2.Text = Replace(TextBox2.Text, encrypta, "1")
    TextBox2.Text = Replace(TextBox2.Text, encryptb, "2")
    TextBox2.Text = Replace(TextBox2.Text, encryptc, "3")

    With kind regards,

    Konrad

    Thanks, that helped me to finalize my program so it can work like in the pictures show below (which was the original question):


    • Edited by Savicava Friday, March 30, 2012 3:53 PM Adding picture with code
    • Marked as answer by Savicava Friday, March 30, 2012 4:57 PM
    Friday, March 30, 2012 3:32 PM
  • I decided to write another replay to show you, what extensions I meant.

    At the moment you just have some variables encrypta, encryptb, encryptc and some hardcoded "1", "2", "3" ... you have 2 times almost the same code ... so it is not that nice ...

    So first we want to store the encryption "key" - something that must be known to encrypt or decrypt something. I mentioned that it could be an Array:

    Dim EncryptionKey(,) As String = New String(2, 1) {{"a", "1"}, {"b", "2"}, {"c", "3"}}

    So we got an Array now which contains pairs ...

    Now we can write a function that translates a given text - and which takes a parameter that tells it if we want to decrypt or encrypt:
        Function Translate(ByRef text As String, ByRef Encrypt As Boolean)
            'Define if we encrypt or decrypt
            Dim source As Integer
            Dim target As Integer
            If (Encrypt) Then
                source = 0
                target = 1
            Else
                source = 1
                target = 0
            End If

            ' Do the replacements
            Dim result As String = text
            For i = 0 To 2
                result = Replace(result, EncryptionKey(i, source), EncryptionKey(i, target))
            Next

            'return the result
            Return result
        End Function

    So when we encrypt, we look for the first item in the array (which is number 0) and replace it with the second (which is the number 1). If we decrypt, it is the other way round.

    And then we simply loop through all pairs (we have 3 pairs ... numbered from 0 to 2) and do the replace.

    I hope this is shows a little bit, what I meant.

    With kind regards,

    Konrad

    Malfunction occurs if you use (in this program) letters "S", "u", "c", "e" and "s" inside quotation marks for "Replace" function like in the examples below(*):

    *Note that in this case only decryption doesn't work properly.

    So thanks again for helping me. Now i can work on my program to make it better...

    Friday, March 30, 2012 4:56 PM
  • Just a note about "secret codes".  Traditionaly, one of the easyest of all codes to break is a "letter substitution code".  That is one where a letter is substituted for another one.  "k for "e", for example.  Or, a number substituted for a letter; "26" for "a", for example.  Such substitution codes are easily broken by statistical means... counting and noting the occurence of each subsitution.  For instance the letter "e" is the most used in the English language, so if a particualr sysmbol, letter, number is most prevelent in an encrypted string, it is most likely an "e", and so on.  If the letter "e" was to be replaced with several symbols; "@!#$%%%^", it would take longer, but the code would be easily broken also.

    One of the things I would do, if making an encryption sceame, is to come up with a method that thwarts statistical means, and have more than one level of encription...having one layer that would confuse a successful attempt at code breaking.  For instance, if the code were to be figured out, have that level of code be something that confused the breaker...as example, not using English, but use Icelandic or another language(Navaho Wind Talkers), that would lead to the breaking thinking he had not broken the code. 

    Saturday, April 14, 2012 5:24 PM