Sentence Encryption in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • culture
    New Member
    • Oct 2008
    • 5

    Sentence Encryption in C++

    I have a problem in C++. I am writing a program to convert a sentence to a string of numbers. The encryption technique is:
    A=1, B=2, C=3...Z=26
    a=33, b=34, c=35...z=58

    I am not suppose to convert spaces they should be left in the string. So, say for example, a user typed the sentence: The Cat

    My final program should display:

    Encrypted: 204037 33352
    Unencrypted: The Cat

    Please can someone help me with this task, as soon as possible. Thank you
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Let me try to restate your problem to see if I understand it:
    Iterate through each character of a string: if the character is an upper or lowercase letter then emit two decimal digits (as per encoding rules); else emit the character itself.

    What if there are pairs of decimal digits in the input string? Looks like they will pass through unchanged to the encoded output string. Your decoder will interpret them as a single alphabetic character.

    Your example suggests that the first 9 uppercase letters expand into a single decimal digit. If so, then I don't see how you can expect to decode the output string.

    Comment

    • culture
      New Member
      • Oct 2008
      • 5

      #3
      If a user enters a sentence, the program should display both the encrypted and unencrypted output.

      Therefore, the input is a string input, no numbers
      And it can contain both upper and lower cases

      A=1, B=2, C=3, D=4, E=5, F=6 and so on...
      a=33, b=34, c=35, d=36, e=37 and so on...

      So, if a user enters the sentence
      The cat
      the output would be:

      Encrypted: 204037 33352
      Unencrypted: The cat

      Comment

      • culture
        New Member
        • Oct 2008
        • 5

        #4
        Originally posted by donbock
        Let me try to restate your problem to see if I understand it:
        Iterate through each character of a string: if the character is an upper or lowercase letter then emit two decimal digits (as per encoding rules); else emit the character itself.

        What if there are pairs of decimal digits in the input string? Looks like they will pass through unchanged to the encoded output string. Your decoder will interpret them as a single alphabetic character.

        Your example suggests that the first 9 uppercase letters expand into a single decimal digit. If so, then I don't see how you can expect to decode the output string.
        If a user enters a sentence, the program should display both the encrypted and unencrypted output.

        Therefore, the input is a string input, no numbers
        And it can contain both upper and lower cases

        A=1, B=2, C=3, D=4, E=5, F=6 and so on...
        a=33, b=34, c=35, d=36, e=37 and so on...

        So, if a user enters the sentence
        The cat
        the output would be:

        Encrypted: 204037 33352
        Unencrypted: The cat

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          What is the encrypted string for:
          The 12 cats

          What is the encrypted string for:
          The L cats

          When you print the unencrypted string are you simply remembering the input string or are you pushing the encrypted string through a decrypter algorithm?

          If you're running a decrypter algorithm then the encryption algorithm needs to be lossless.

          Comment

          • AmeL
            New Member
            • Oct 2008
            • 15

            #6
            As you mentioned above, the designed constraint only allows the string input and never number type. so i better not speak something about this ( but wait you should consider this case too before shipping your .exe to your customer).
            Yet I found another problem with this
            How can u decrypt this data : 334 ?
            if you split like this 3, 3 and 4 so it will be CCD
            if that comes to 3, and 34 so it will be Cb
            and if that 33, and 4 so it will be aD
            see ? how can you manage that ?

            Well, i would like to say that this is not called the encryption at all; that is a hashing. You do a hashing value by replacing the real value with the representing value; so do you think that it is more secured enough ?
            Let says you are the one, on this earth, who knows this algorithm ( replace a letter with an appropriate number), so at least there is one person that can decrypt the data; no doubt that person is you.

            Well the encryption means in another way. Even the one who designs the code for encryption can not reveal the decrypted data. why ? Actually there are so many mechanisms to achieve that goal but this is one of those :
            You better implement a key associated with the decrypted data which looks like the following :
            Code:
            int Encrypt(char *key, char *data)
            {
               // do something with the key and associate it with
               // data to create the decrypted data
               decrypted = key + data   // example
               return ....
            }
            So imagine, if you were the code designer, you would not know what the data is.
            why ? because it uses the key to encrypt the data. You can know what is behind unless you know the key.

            Comment

            • curiously enough
              New Member
              • Aug 2008
              • 79

              #7
              This is a useful program that will make the encryption decodable after it is coded:
              Code:
              Deleted by moderator
              This program will give you a file called zaza which has written in it the encrypted data, which in the case of "The Cat" which is used in this program is: {20,40,37,-32,3,33,52,0}. I just fixed the ascii code to the way you want(ascii value - 64). And note that I placed -32 for the space to avoid complications, and I placed a 0 for the null charachter.
              Now that you have commas between all the numbers you will have no problem decoding it, just copy the code from the file zaza and paste it at the right of char string[]= in this program:
              Code:
              Spoonfeeding code removed
              But to have an actual code that could not be decrypted easily apply some kind of formula to the numbers, like 2*number+1 in the encryption code which will make you use (number-1)/2 in the decryption code, but ofcourse there are much better ways to complicate the encrypted code, just use your imagination.

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Yes, I have to agree with the above poster, this is not encryption it is just replacing letters with numbers. Replacing letters with numbers is also known as a Caesar cipher and can be broken almost instantly with today's computers. All you need to do is take the char value and cast it to an int and then add or subtract the appropriate offset.

                If you are using ASCII the characters A B C correspond to 65,66,67 so when you read them in to your program as integers you will have 65, 66 and 67. Then you simply subtract 64 from each value and then A will correspond to 1 and B will correspond to 2 and C will correspond to 3.

                Good luck, and be careful using other people's code as it may cause errors that you are unfamiliar with.

                Comment

                Working...