Decoding a secret message:
The description may seem daunting, but the solution is not that hard. You can use the built-in string datatype with the associated built-in functions and while loop (with ‘len’ function) or a for loop (with ‘in’ operator) to traverse the string. Also, use the ’chr’ and ’ord’ functions (which are based on ASCII code) discussed in course material. Make sure to look at the examples in the course material and do #18 and #19 in Exercises 2. Answer for #19 is provided and it can give valuable hints for solving this problem.
Your country is at war and your enemies are using a secret code to communicate with each other. You have managed to intercept a message that read as follows:
:mmZ\dxZmx]Zpgy
The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code (you can find this set easily by searching online). Individual characters in a string are encoded using this system. For example, the letter ‘A’ is encoded using the number 65 and ‘B’ is encoded using the number 66.
Your enemy’s secret code takes each letter of the message and encrypts it as follows (using a secret key):
If (OriginalChar + Key > 126) then
EncryptedChar = ((OriginalChar + Key) - 127) + 32
Else
EncryptedChar = (OriginalChar + Key)
For example, if the enemy uses Key = 10 then the message ”Hey” would be encrypted as:
Character ASCII
H 72
e 101
y 121
Encrypted H = (72 + 10) = 82 = R in ASCII
Encrypted e = (101 + 10) = 111 = o in ASCII
Encrypted y = 32 + ((121 + 10) - 127) = 36 = $ in ASCII
Consequently, “Hey” would be transmitted as “Ro$”.
Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish.
HINT: You will need to implement a decrypt function that takes in an encrypted message as string and a key as integer and returns the decrypted message as string. You can decrypt each letter of the message as follows:
If (EncryptedChar - Key < 32) then
DecryptedChar = ((EncryptedChar - Key) + 127) - 32
Else
DecryptedChar = (EncryptedChar - Key)
NOTE: You should also implement an encrypt function that takes in a regular message as string and a key as integer and returns the corresponding encrypted message as string (the algorithm to encrypt a message is mentioned above in the problem description). This function would help you in encrypting any regular message, which then can be passed to your decrypt function to be decrypted.
For Encryption: You should ask the user for any regular message and a key and output the corresponding encrypted message.
Sample run:
Enter a regular message to encode:
Attack at dawn!
Enter a key value (between 0 and 100) for encoding:
88
The encoded message is:
:mmZ\dxZmx]Zpgy
For Decryption: You should ask the user for an encrypted message and output 100 well-formatted, decrypted messages (using keys between 1 and 100) along with the corresponding key value.
Sample run (the gibberish messages below are not accurate):
Enter an encrypted message to decode:
:mmZ\dxZmx]Zpgy
The following are the decoded messages for keys 1 to 100:
Key: 1 –> Decoded Message: whfuihwuiidh89
Key: 2 –> Decoded Message: 9ehkaOY3ewine
...
Key: 87 –> Decoded Message: Buubdl!bu!ebxo”
Key: 88 –> Decoded Message: Attack at dawn!
...
Key: 100 –> Decoded Message: on3dwp389/wi8
string
datatype with the associated built-in functions andwhile
loop (with 'len
' function) or afor
loop (with 'in
' operator) to traverse the string. Also, use the 'chr
' and 'ord
' functions (which are based on ASCII code). – Brian Cain Nov 14 '13 at 17:43string.make_trans('plaintext', 'ciphertex')
in thestring
library. – Ashish Nitin Patil Nov 14 '13 at 17:44encryptedChar
? it tells you exactly how to get the encrypted char ... – Joran Beasley Nov 14 '13 at 17:52