# File: caesardec.py # Program to decrypt using Caesar cipher # # Matt Bishop, MHI 289I, Fall 2024 # # # this corresponds to a key of 'D' ('A' = 0, ... 'Z' = 25) # it's near the top so we can find (and change) it easily # key = 3 # # the decipherment function # def decipher(c, k): return (c + 26 - k) % 26 # # now the main routine # def main(): # ask user for input message (plaintext) try: cipher = input("Enter your message here: ") except: return # initialize output (ciphertext) plain = "" # # encipher each character and append it to current ciphertext # for i in cipher: # leave non-letters alone if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": # map the letter into 0 . . 25 n = ord(i) - ord('A') # shift it p = decipher(n, key) # map it back into a letter lett = chr(ord('A') + p) elif i in "abcdefghijklmnopqrstuvwxyz": # map the letter into 0 . . 25 n = ord(i) - ord('a') # shift it p = decipher(n, key) # map it back into a letter lett = chr(ord('a') + p) else: lett = i # now append it plain = plain + lett # print it, surrounded by quotes print("'%s' --> '%s'" % (cipher, plain)) # # run the program # main()