Advertisement
aolivens

Python (Shift) Substitution Cipher

Sep 18th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. #=====================================#
  2. #==============MODULES================#
  3. #=====================================#
  4.  
  5.  
  6.  
  7. #=====================================#
  8. #==============VARIABLES==============#
  9. #=====================================#
  10.  
  11. menus = ["(1) Encrypt", "(2) Decrypt", "(3) Exit   "]
  12.  
  13. #=====================================#
  14. #==============FUNCTIONS==============#
  15. #=====================================#
  16.  
  17. #defines menu
  18. def menu(parameter):
  19.     print "     +===========+"
  20.     print "     |   MENU    |"
  21.     print "     |===========|"
  22.     for word in parameter:
  23.         print "     |%s|" % word
  24.         print "     |-----------|"
  25.  
  26. #defines encryption algorithm
  27. def enc(data, key):
  28.     output = ""
  29.     for char in data:
  30.         nums = ord(char) #convert ascii
  31.         nums = (nums + key) #add ascii value and key value to create constant shift
  32.         output = (output + chr(nums)) #create output string with all 'new' characters
  33.         print "The encrypted message is: ", output
  34.  
  35. #defines decryption algorithm
  36. def dec(data, key):
  37.     output = ""
  38.     for char in data:
  39.         nums = ord(char)
  40.         nums = (nums - key)
  41.         output = (output + chr(nums))
  42.         print "The decrypted message is: ", output
  43.    
  44. #=====================================#
  45. #===========MAIN PROGRAM==============#
  46. #=====================================#
  47.  
  48. def main():
  49.     menu(menus)
  50.    
  51.     choice = input("Please make a selection(1-3): ")
  52.    
  53.     #Encryption
  54.     if choice == 1:
  55.         key = input("Please enter numerical key: ")
  56.         data = raw_input("Please enter a message to encrypt: ")
  57.         enc(data, key)
  58.        
  59.     #Decryption
  60.     elif choice == 2:
  61.         key = input("Please enter numerical key: ")
  62.         data = raw_input("Please enter a message to decrypt: ")
  63.         dec(data, key)
  64.        
  65.     #Exit
  66.     elif choice == 3:
  67.         print "Exiting Program. Goodbye."
  68.        
  69.     #Fault
  70.     else:
  71.         print "Invalid Selection."
  72.         return main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement