Advertisement
aolivens

Python XOR String Encryption & Decryption

Sep 13th, 2013
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. #=====================================#
  2. #==============MODULES================#
  3. #=====================================#
  4.  
  5. from Crypto.Cipher import XOR
  6. import base64
  7.  
  8. #=====================================#
  9. #==============VARIABLES==============#
  10. #=====================================#
  11.  
  12. block_size = 8
  13. key_size = 8
  14. menus = ["(1) Encrypt", "(2) Decrypt", "(3) Exit   "]
  15.  
  16. #=====================================#
  17. #==============FUNCTIONS==============#
  18. #=====================================#
  19.  
  20. #creates menu
  21. def menu(parameter):
  22.     print "     +===========+"
  23.     print "     |    MENU   |"
  24.     print "     |-----------|"
  25.     for word in parameter:
  26.         print "     |%s|" % word
  27.         print "     |-----------|"
  28.  
  29. #defines encryption algorithm
  30. def enc(key_size, block_size):
  31.  
  32.     #get key and pad if necessary
  33.     key = raw_input("Please enter a key: ")
  34.     key += (key_size - len(key) % key_size) * chr(key_size - len(key) key_size)
  35.    
  36.     #get data and pad data if necessary
  37.     data = raw_input("Please enter a string: ")
  38.     pad = lambda data: data + (block_size - len(data) % block_size) * chr(block_size - len(data) % block_size)
  39.     data = pad(data)
  40.    
  41.     #define what is needed for XOR
  42.     cipher = XOR.new(key)
  43.    
  44.     #encode using base64 and encrypt data
  45.     output = base64.b64encode(cipher.encrypt(data))
  46.    
  47.     print "The encrypted message is: ", output
  48.  
  49. def dec(key_size, block_size):
  50.    
  51.     #get key and pad if necessary
  52.     key = raw_input("Please enter a key: ")
  53.     key += (key_size - len(key) % key_size) * chr(key_size - len(key) key_size)
  54.    
  55.     #get data and decode using base64
  56.     data = raw_input("Please enter a string: ")
  57.     data = base64.b64decode(data)
  58.    
  59.     #strip off padding
  60.     strippad = lambda data: data[0:-ord(data[-1])]
  61.    
  62.     #define what is needed for XOR
  63.     cipher = XOR.new(key)
  64.    
  65.     #strip off padding and then decrypt
  66.     output = strippad(cipher.decrypt(data))
  67.    
  68.     print "The decrypted message is: ", output
  69.  
  70. #=====================================#
  71. #===========MAIN PROGRAM==============#
  72. #=====================================#
  73.  
  74. def main():
  75.     menu(menus)
  76.     choice = input("Please make a selection(1-3): ")
  77.    
  78.     #Encryption
  79.     if choice == 1:
  80.         enc(key_size, block_size)
  81.     #Decryption
  82.     elif choice == 2:
  83.         dec(key_size, block_size)
  84.     #Exit
  85.     elif choice == 3:
  86.         print "Exiting Program. Goodbye!"
  87.         print
  88.         print
  89.         return
  90.     #Invalid Choice
  91.     else:
  92.         print "ERROR. Invalid Selection. "
  93.         print
  94.         print
  95.         return main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement