# File: caesardec.py # Program to encrypt using Caesar cipher # # Matt Bishop, ECS 10, Spring 2012 # # # 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 encipherment function # def encipher(p, k): return (p + k) % 26 # # now the main routine # def main(): # ask user for input message (plaintext) try: plain = input("Enter your message here: ") except: return # initialize output (ciphertext) cipher = "" # # encipher each character and append it to current ciphertext # for i in plain: # leave non-letters alone if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": # map the letter into 0 . . 25 n = ord(i) - ord('A') # shift it c = encipher(n, key) # map it back into a letter lett = chr(ord('A') + c) elif i in "abcdefghijklmnopqrstuvwxyz": # map the letter into 0 . . 25 n = ord(i) - ord('a') # shift it c = encipher(n, key) # map it back into a letter lett = chr(ord('a') + c) else: lett = i # now append it cipher = cipher + lett # print it, surrounded by quotes print("'%s' --> '%s'" % (plain, cipher)) # # run the program # main()