Study the \"circular Caesar cipher\" problem presented in Programming Exercises
ID: 3923680 • Letter: S
Question
Study the "circular Caesar cipher" problem presented in Programming Exercises 7 and 8 of Book Chapter 5. Write a Python program to encode the plain text message and another Python program to decode the encoded message. Use the following as starter code: # S Caesar Cipher: Encoder # Do not modify next 8 lines def encode0 print( Caesar cipher) printo chars is given for your convenience. It will help for # an easier solution. You do not have to use it. chars - "ABCDEFGHUKLMNOPORSTUVWxYZ abcdefghijklmnoparstuvwxyz # Fill in the expression to ask for the key value: key- # Fill in the expression to ask for the phrase to encode: plain- # Your main code starts here. Use as many lines as needed. a Do not modify any lines below print("Encoded message follows print(cipher) encode)Explanation / Answer
def encryptOrDecrypt(msg, shift):
msg = msg.lower()
encryptedText = ""
for c in msg:
if c in "abcdefghijklmnopqrstuvwxyz":
num = ord(c)
num += shift
if num > ord("z"): # wrap if necessary
num -= 26
elif num < ord("a"):
num += 26
encryptedText = encryptedText + chr(num)
else:
encryptedText = encryptedText + c
return encryptedText
def encrypt(msg,shift):
return helper(msg, shift)
def decrypt(msg,shift):
return helper(msg,-1* shift)
# main program
msg = input()
shift = -1
while(shift < 0)
shift = input()
encryptedText = encrypt(msg,shift)
print("The encoded msg is:", encryptedText)
msg = decrypt(encryptedText,shift)
print("The decoded msg is:", msg)