Advertisement
aolivens

Python Caesar Cipher Encryption & Decryption

Sep 18th, 2013
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. #=====================================#
  2. #==============MODULES================#
  3. #=====================================#
  4.  
  5.  
  6.  
  7. #=====================================#
  8. #==============VARIABLES==============#
  9. #=====================================#
  10.  
  11. alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  12. alpha_size = len(alpha)
  13. menus = ["(1) Encrypt", "(2) Decrypt", "(3) Exit   "]
  14. max_set = ord("Z")
  15. min_set = ord("A")
  16.  
  17. #=====================================#
  18. #==============FUNCTIONS==============#
  19. #=====================================#
  20.  
  21. def menu(parameter):
  22.     print "     +===========+"
  23.     print "     |   MENU    |"
  24.     print "     |-----------|"
  25.     for word in parameter:
  26.         print "     |%s|" % word
  27.         print "     |-----------|"
  28.  
  29. #define encryption/decryption algorithm
  30. def algo(data, key):
  31.     output = ""
  32.     for char in data: #iterates through each character in string
  33.         if char.isalpha(): #if character is True....
  34.             nums = ord(char) #convert character to ascii number
  35.             nums = (nums + key) #shift ascii number with key
  36.             if nums > max_set: #if new ascii character higher than ascii "Z"
  37.                 nums = (nums - alpha_size) #subtract 26 to bring character in Modulo 26
  38.             elif nums < min_set: #if new ascii character lower than ascii "A"
  39.                 nums = (nums + alpha_size) #add 26 to bring the character in Modulo 26
  40.             output = (output + chr(nums)) #append blank string with converted ascii to new character
  41.         else:
  42.             output = (output + char) #append string with special characters and numbers b/c of established modulo
  43.     print output
  44.  
  45. #=====================================#
  46. #===========MAIN PROGRAM==============#
  47. #=====================================#
  48.  
  49. def main():
  50.     menu(menus)
  51.    
  52.     choice = input("Please make a selection(1-3): ")
  53.    
  54.     #Encryption
  55.     if choice == 1:
  56.         key = input("Please enter a key(0-25): ")
  57.         data = raw_input("Please enter a message to encrypt: ").upper()
  58.         print "The encrypted message is: "
  59.         algo(data, key)
  60.        
  61.     #Decryption
  62.     elif choice == 2:
  63.         key = input("Please enter a key(0-25): ")
  64.         key = (key * -1)
  65.         data = raw_input("Please enter a message to decrypt: ").upper()
  66.         print "The decrypted message is: ")
  67.         algo(data, key)
  68.    
  69.     #Exit
  70.     elif choice == 3:
  71.         print "Exiting Program. Goodbye"
  72.         return
  73.    
  74.     #Fault
  75.     else:
  76.         print "Invalid Selection"
  77.         return main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement