Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python decoding a secret message Decoding a secret message: The description may

ID: 3762181 • Letter: P

Question

python decoding a secret message

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:

:mmZdxZmx]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:

:mmZdxZmx]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:

:mmZdxZmx]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

Explanation / Answer

def encrypt(mess,key): encoded = "" for i in range(0,len(mess)): val = ord(mess[i]) if(val + key > 126): encoded += chr(val + key - 127 + 32) else: encoded += chr(val + key) return encoded def decrypt(mess,key): orgi = "" for i in range(0,len(mess)): val = ord(mess[i]) if (val - key < 32): orgi += chr(((val - key )+ 127) - 32) else: orgi += chr(val - key) return orgi while(1): n = int(input("Enter 1 to encrypt, 2 to decrypt, 0 to quit: ")) if(n==1): mess = input("Enter the message to be encerypt: ") key = int(input("Enter a key value (between 0 and 100) for encoding: ")) print("The encoded message is: "+encrypt(mess,key)) elif(n==2): mess = input("Enter an encrypted message to decode: ") print("The following are the decoded messages for keys 1 to 100:") for i in range(1,101): print("key: "+str(i)+" Decoded Message: "+decrypt(mess,i)) elif(n==0): break