locked
"Secret" code translator

    Question

  • The title may seem odd in what i'm trying to ask.

     

    I'm trying to come up with a way to translate code, example: S lyyq pyzx which translates to I need help

    User is asked to enter the code OR the message to translate, which if it's in the code, it's outputs the message or if the user enters a message it translates to the code.

     

    so far i'v come up with the simple stuff, but the other stuff is far out of my current knowledge.

    This is what i have:

    Dim translatedCode As String
    Dim codeToTranslate As String
    Dim letters() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
    Console.WriteLine("Enter code:")
    codeToTranslate = Console.ReadLine()
    

    It's not much, but my knowledge of Visual Basic goes as far as a semester of introductory work.

    I've searched a lot of places for help, but turned up empty.

     

    Thanks in advance for any and all help!

    Wednesday, November 16, 2011 7:17 AM

Answers

  • That's very good, my friend, you did well with this code !

    This line seems unnecessary :

    Dim letters() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

    After careful thinking, I now believe the Dictionary approach is excellent when doing Encoding. But it is not very helpful when doing Decoding.

    So I would like to offer another method of encoding/decoding based on regular strings.

    Say your "S lyyq pyzx" string is decoded to "I need help". So you can build two strings like this :

    Dim String1 as String = "SLYQPZX"

    Dim String2 as string = "INEDHLP"

    You can see that a "pair" of characters is composed of two letters at the same position in the strings.

    Therefore, to Encode, you will search each character in String2. Then you can retrieve the corresponding Char in String1.

    For i as Integer = 0 to IncomingString.Length -1

         Dim IncomingChar as Char = IncomingString(i)

         Dim pos as Integer = String2.IndexOf(IncomingChar)

         Dim OutGoingChar as Char = String1(pos)

         Result &= OutGoingChar

    Next

     

    To Decode, you simply exchange the role of String1 and String2 :

    For i as Integer = 0 to IncomingString.Length -1

         Dim IncomingChar as Char = IncomingString(i)

         Dim pos as Integer = String1.IndexOf(IncomingChar)

         Dim OutGoingChar as Char = String2(pos)

         Result &= OutGoingChar

    Next

    • Marked as answer by ProgramMonkey Thursday, November 17, 2011 5:19 AM
    Wednesday, November 16, 2011 8:27 PM

All replies

  • There are quite a few approaches here.

     

    The simplest would probably be to use a pair of Dictionary(Of Char, Of Char) that map from message -> code, and code->message.  You could then just do the lookup character by character, and write out the result. 


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, November 16, 2011 5:45 PM
  • In your example, the encoding is not reciprocal. In other words, if you translate "I need help", it won't be "S lyyq pyzx". So you need to enter the text message and also a value indicating whether you want to encode or decode.

    Reed is right, you may want to use a Dictionary (Of Char, Char).

    Read each character of the incoming string, with a loop like :

    For i as Integer = 0 to IncomingString.Length -1

         Dim theChar as Char = IncomingString(i)

    Next

    Depending whether you are encoding or decoding

    Then, for each character, find it in the dictionary and retrieve the corresponding character.

    Concatenate those characters to obtain the final result.

    Wednesday, November 16, 2011 6:05 PM
  • In your example, the encoding is not reciprocal. In other words, if you translate "I need help", it won't be "S lyyq pyzx". So you need to enter the text message and also a value indicating whether you want to encode or decode.

    Reed is right, you may want to use a Dictionary (Of Char, Char).

    Read each character of the incoming string, with a loop like :

    For i as Integer = 0 to IncomingString.Length -1

         Dim theChar as Char = IncomingString(i)

    Next

    Depending whether you are encoding or decoding

    Then, for each character, find it in the dictionary and retrieve the corresponding character.

    Concatenate those characters to obtain the final result.


    So you're saying i should make it ask whether the user is entering code to decrypt or a message to encrypt and depending on what the user enters, it should call up a function or sub to convert?

     

    So i would have something like....

    Module Name_Of_Module
    
    Sub Main()
    
    Dim codeOrMessage As String
    Dim translatedCode As String
    Dim codeToTranslate As String
    Dim letters() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
    Console.WriteLine("Are you enetering code or message?")
    codeOrMessage = Console.ReadLine.ToLower()
    Console.Clear()
    
    Select Case codeOrMessage
    Case "code"
    encryptMessage()
    Case "message"
    decryptCode()
    End Select
    
    
    End Sub
    
    Sub encryptMessage()
    Dim messageToEncrypt As String
    Dim encryptedMessage As String
    Console.WriteLine("Enter message:")
    messageToEncrypt = Console.ReadLine.ToLower()
    Console.Clear()
    For i as Integer = 0 to messageToEncrypt.Length -1
    
    Dim theChar as Char = messageToEncrypt(i)
    
    Next
    
    'The dictionary thing i'm still not
    'understanding. Can you guys help me figure that part out?
    
    Console.WriteLine(encryptedMessage)
    End Sub
    
    
    Sub decryptCode()
    Dim codeToDecrypt As String
    Dim decryptedMessage As String
    Console.WriteLine("Enter code:")
    codeToDecrypt = Console.ReadLine.ToLower()
    Console.Clear()
    
    For i as Integer = 0 to codeToDecrypt.Length -1
    
    Dim theChar as Char = codeToDecrypt(i)
    
    Next
    
    
    'The dictionary thing i'm still not
    'understanding. Can you guys help me figure that part out?
    
    Console.WriteLine(decryptedCode)
    End Sub
    
    End Module

     

    • Edited by ProgramMonkey Wednesday, November 16, 2011 7:37 PM tweek code to ad Sygrien's code bit
    Wednesday, November 16, 2011 6:34 PM
  • That's very good, my friend, you did well with this code !

    This line seems unnecessary :

    Dim letters() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

    After careful thinking, I now believe the Dictionary approach is excellent when doing Encoding. But it is not very helpful when doing Decoding.

    So I would like to offer another method of encoding/decoding based on regular strings.

    Say your "S lyyq pyzx" string is decoded to "I need help". So you can build two strings like this :

    Dim String1 as String = "SLYQPZX"

    Dim String2 as string = "INEDHLP"

    You can see that a "pair" of characters is composed of two letters at the same position in the strings.

    Therefore, to Encode, you will search each character in String2. Then you can retrieve the corresponding Char in String1.

    For i as Integer = 0 to IncomingString.Length -1

         Dim IncomingChar as Char = IncomingString(i)

         Dim pos as Integer = String2.IndexOf(IncomingChar)

         Dim OutGoingChar as Char = String1(pos)

         Result &= OutGoingChar

    Next

     

    To Decode, you simply exchange the role of String1 and String2 :

    For i as Integer = 0 to IncomingString.Length -1

         Dim IncomingChar as Char = IncomingString(i)

         Dim pos as Integer = String1.IndexOf(IncomingChar)

         Dim OutGoingChar as Char = String2(pos)

         Result &= OutGoingChar

    Next

    • Marked as answer by ProgramMonkey Thursday, November 17, 2011 5:19 AM
    Wednesday, November 16, 2011 8:27 PM
  • Here's one simple way to do it.  This is just a simple substitution code.  Don't use it for any real security, since it can be broken in seconds or less!

        Public Function EncryptDecryptString(ByVal inputString As StringOptional ByVal decrypt As Boolean = FalseAs String
            Dim sourceChars As String = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
            Dim resultChars As String = "36N8lkXruq94jMZInPpshR xHc2mTQb7eYai5vGWDzFdoC0wKSBt1EOgVALJfUy"   'I used a random shuffling of the characters
     
            Dim result As String
            If decrypt Then
                result = New String(inputString.Select(Function(c) sourceChars(resultChars.IndexOf(c))).ToArray())
            Else
                result = New String(inputString.Select(Function(c) resultChars(sourceChars.IndexOf(c))).ToArray())
            End If
     
            Return result
        End Function
    

    This code calls the Select method on the input string which is an extension method.  For each character in the string, it does a lookup in the resultChars array and gets the corresponding character.  So for the letter H, it returns u.  The Select methods returns an array of type Char which is used to create a new string. 

    Chris

    Wednesday, November 16, 2011 8:28 PM

  • So you're saying i should make it ask whether the user is entering code to decrypt or a message to encrypt and depending on what the user enters, it should call up a function or sub to convert? 

    You need to decide just what your task is.

    If you encrypt a message then presumably you want to be able to decrypt it. Do you want to do that by passing a key that the recipient then uses to decrypt the message? If you use a standard encryption tool then anyone else who gets hold of that key will also be able to decrypt the message, so you would have to be able to send it securely. Or, do you want the user to be able to decrypt the messge without a key? This means that your software knows how to decrypt any message that you have encrypted, so anyone who has that software can decrypt it, but no-one else could (there is no key to steal) unless they can work out how your software does it and write their own version.

    If you are encrypting for your own use only, then you have a chance of keeping the key secret and you can use almost any routine that uses a key, with encrypt and decrypt code in the same application. If you want someone else to be able to decrypt it then you have to provide them with the tools to do so, or use an encryption scheme for which they already have the software.

    If you use a key to decrypt, should it be the same key as was used to encrypt? Should the sender need to know the key that the recipient will use to decrypt the message, or should they be kept secret from each other (so that the sender knows that only the correct recipient can read the message, and the recipient knows that only the correct sender could have sent the message)?

    How secure does this encryption need to be?

    But you might just want to conceal some information. You can do this in a way that enables you to confirm the information without knowing what it is. This would be how you would handle passwords - you would encrypt the password using a rule, then when someone is asked for their password you would encrypt what they enter and compare it to what was original created from the rule. If the results match, you know that the same password was entered, but the encrypted password did not need to be decrypted to prove that. Therefore, you rule can be more complex because it does not have to be reversible.

    Thursday, November 17, 2011 2:35 AM
  • Thanks everyone! I got it down! It's now complete. I may tweek it a bit to be more secretive, like i made a small menu driven log in forum that did a few small things. I guess i could add the encrypt/decrypt to it. :D

     

    Thanks again everyone!!!!!

    Thursday, November 17, 2011 5:26 AM