Write a program to do the enciphering. It should prompt the user for a message,
ID: 3557371 • Letter: W
Question
Write a program to do the enciphering. It should prompt the user for a message, and print out both the entered message and its corresponding ciphertext.
Input. The message to be enciphered. Here is what a correct input should look like (the red text is what you type):
Output. The message to be enciphered and the corresponding ciphertext. Here is what the output corresponding to the above input should look like:
(note the double quotes around the plaintext and ciphertext).
Here is another example. The input is:
and the corresponding output is:
Now write a program to decipher the messages. Again, it should prompt the user for a ciphertext, and print out both the entered ciphertext and the corresponding plaintext.
Input. The message to be deciphered. Here is what a correct input should look like (the red text is what you type):
Output. The message to be deciphered and the corresponding plaintext. Here is what the output corresponding to the above input should look like:
(note the double quotes around the ciphertext and plaintext).
Here is another example. The input is:
and the corresponding output is:
Now we will combine the two parts you just did into a single program. This program asks the user to type
Explanation / Answer
# http://ideone.com/Wc4wXR
def encipher(msg):
l = len(msg)
cipher = ''
for i in range(0,l,2):
cipher = cipher+msg[i]
for i in range(1,l,2):
cipher = cipher+msg[i]
return cipher
def decipher(cipher):
l1 = len(cipher)
l = int(l1/2)
msg = ''
for i in range(l):
msg = msg+cipher[i]
if l1%2 != 0:
msg = msg+cipher[l+i+1]
else:
msg = msg+cipher[l+i]
if l1%2 != 0:
msg = msg+cipher[l]
return msg
if __name__=='__main__':
msg = raw_input('Enter plain text: ')
print('Plain = "{0}"'.format(msg))
print('Cipher = "{0}"'.format(encipher(msg)))
msg = raw_input('Enter cipher text: ')
print('Cipher = "{0}"'.format(msg))
print('Plain = "{0}"'.format(decipher(msg)))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.