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

Language is Python You will write a program that is a customizable Caesar Cipher

ID: 3840494 • Letter: L

Question

Language is Python

You will write a program that is a customizable Caesar Cipher program. First, the program will take in from the user the shift of the cipher. Caesar Ciphers can be defined as having a "shift" as part of its key. For example, if the shift is 1: a rightarrow b, b rightarrow c, ... z rightarrow a Or, if it is 2: a rightarrow c, b rightarrow d, ..., x rightarrow z, y rightarrow a, z rightarrow b Then, it will loop, at each iteration asking the user whether they want to exit, encrypt a string, or decrypt a string. If not exiting, then printing the result. There should be functions for both encrypting and decrypting. Example: Output: Shift: Input: 1 Output: encrypt, decrypt, or exit? Input: encrypt Output: string to encrypt: Input:

Explanation / Answer

Program (explanation in comments):

import sys

def encrypt(s, shift):
e_str = ""
for c in s:
#ignore all invalid characters only check for lower and upper case alphabets
if ord(c) >= ord('a') and ord(c) <= ord('z'):
# function ord(c) finds the ascii value of character c
# ord(c) - ord('a') will find the relative position of character w.r.t 'a'
# shift the relative position ahead by 'shift'
e_str += chr(ord('a') + (ord(c) - ord('a') + shift)%26)
if ord(c) >= ord('A') and ord(c) <= ord('Z'):
e_str += chr(ord('A') + (ord(c) - ord('A') + shift)%26)
return e_str

def decrypt(s, shift):
d_str = ""
for c in s:
#ignore all invalid characters only check for lower and upper case alphabets
if ord(c) >= ord('a') and ord(c) <= ord('z'):
# function ord(c) finds the ascii value of character c
# ord(c) - ord('a') will find the relative position of character w.r.t 'a'
# shift the relative position back by 'shift'
d_str += chr(ord('a') + (ord(c) - ord('a') - shift)%26)
if ord(c) >= ord('A') and ord(c) <= ord('Z'):
d_str += chr(ord('A') + (ord(c) - ord('A') - shift)%26)
return d_str

print "Shift: "
shift_str = sys.stdin.readline()
shift = int(shift_str)

while 1:
print "encrypt, decrypt or exit?"
op = sys.stdin.readline()
op = op[:-1]
if op == "exit":
break
if op == "encrypt":
print "String to encrypt: "
ip_str = sys.stdin.readline()
ip_str = ip_str[:-1]
e_str = encrypt(ip_str, shift)
print "Encrypted string: ", e_str
if op == "decrypt":
print "String to decrypt: "
ip_str = sys.stdin.readline()
ip_str = ip_str[:-1]
d_str = decrypt(ip_str, shift)
print "Decrypted string: ", d_str

SAMPLE OUTPUT

Shift:                                                                                                                                                         

1                                                                                                                                                              

encrypt, decrypt or exit?                                                                                                                                      

encrypt                                                                                                                                                        

String to encrypt:                                                                                                                                             

Abc                                                                                                                                                            

Encrypted string:  Bcd                                                                                                                                         

encrypt, decrypt or exit?                                                                                                                                      

decrypt                                                                                                                                                        

String to decrypt:                                                                                                                                             

bcd                                                                                                                                                            

Decrypted string:  abc                                                                                                                                         

encrypt, decrypt or exit?

decrypt                                                                                                                                                        

String to decrypt:                                                                                                                                             

abc                                                                                                                                                            

Decrypted string:  zab                                                                                                                                         

encrypt, decrypt or exit?

exit