Advertisement
aolivens

Python ARC4 String Encryption & Decryption

Sep 13th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. #=====================================#
  2. #==============MODULES================#
  3. #=====================================#
  4.  
  5. from Crypto.Cipher import *
  6. from Crypto import Random
  7. import base64
  8.  
  9. #=====================================#
  10. #==============VARIABLES==============#
  11. #=====================================#
  12.  
  13. mtypes = ["(1) Encrypt", "(2) Decrypt", "(3) Exit   "]
  14. block_size = 8
  15. key_size = 8
  16.  
  17. #=====================================#
  18. #==============FUNCTIONS==============#
  19. #=====================================#
  20.    
  21. #create menu
  22. def menu(parameter):
  23.     print "         +===========+"
  24.     print "         |    MENU   |"
  25.     print "         |-----------|"
  26.     for word in parameter:
  27.         print "         |%s|" % word
  28.         print "         |-----------|"
  29.  
  30. #defines encryption algorithm
  31. def enc(key_size, block_size):
  32.    
  33.     #get key and pad if necessary
  34.     key = raw_input("Please enter a key: ")
  35.     key += (key_size - len(key) % key_size) * chr(key_size - len(key) % key_size)
  36.    
  37.     #get data and pad if necessary
  38.     data = raw_input("Please enter a string to encrypt: ")
  39.     pad = lambda data: data + (block_size - len(data) % block_size) * chr(block_size - len(data) % block_size)
  40.     data = pad(data)
  41.    
  42.     #define what is needed for DES
  43.     cipher = ARC4.new(key)
  44.    
  45.     #combine IV and encrypted text | encode using base64
  46.     output = base64.b64encode(iv + cipher.encrypt(data))
  47.  
  48. #defines decryption algorithm          
  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 base64
  56.     data = raw_input("Please enter a string to decrypt: ")
  57.     data = base64.b64decode(data)
  58.  
  59.     #strip padding
  60.     strippad = lambda data: data[0:-ord(data[-1])]
  61.  
  62.     #define what is needed for DES
  63.     cipher = ARC4.new(key)
  64.  
  65.     #strip off padding and decrypt data
  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(mtypes)
  76.     mtypes_choice = input("Please select to encrypt, decrypt, or exit(1-3): ")
  77.    
  78.     #Encryption
  79.     if mtypes_choice == 1:
  80.         enc(key_size, block_size)
  81.        
  82.     #Decryption
  83.     elif mtypes_choice == 2:
  84.         dec(key_size, block_size)
  85.        
  86.     #Exit
  87.     elif mtypes_choice == 3:
  88.         print "Exiting Program. Goodbye."
  89.         print
  90.         return
  91.        
  92.     #Fault
  93.     else:
  94.         print "ERROR. Invalid Selection."
  95.         print
  96.         print
  97.         return main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement