Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #=====================================#
- #==============MODULES================#
- #=====================================#
- from Crypto.Cipher import XOR
- import base64
- #=====================================#
- #==============VARIABLES==============#
- #=====================================#
- block_size = 8
- key_size = 8
- menus = ["(1) Encrypt", "(2) Decrypt", "(3) Exit "]
- #=====================================#
- #==============FUNCTIONS==============#
- #=====================================#
- #creates menu
- def menu(parameter):
- print " +===========+"
- print " | MENU |"
- print " |-----------|"
- for word in parameter:
- print " |%s|" % word
- print " |-----------|"
- #defines encryption algorithm
- def enc(key_size, block_size):
- #get key and pad if necessary
- key = raw_input("Please enter a key: ")
- key += (key_size - len(key) % key_size) * chr(key_size - len(key) key_size)
- #get data and pad data if necessary
- data = raw_input("Please enter a string: ")
- pad = lambda data: data + (block_size - len(data) % block_size) * chr(block_size - len(data) % block_size)
- data = pad(data)
- #define what is needed for XOR
- cipher = XOR.new(key)
- #encode using base64 and encrypt data
- output = base64.b64encode(cipher.encrypt(data))
- print "The encrypted message is: ", output
- def dec(key_size, block_size):
- #get key and pad if necessary
- key = raw_input("Please enter a key: ")
- key += (key_size - len(key) % key_size) * chr(key_size - len(key) key_size)
- #get data and decode using base64
- data = raw_input("Please enter a string: ")
- data = base64.b64decode(data)
- #strip off padding
- strippad = lambda data: data[0:-ord(data[-1])]
- #define what is needed for XOR
- cipher = XOR.new(key)
- #strip off padding and then decrypt
- output = strippad(cipher.decrypt(data))
- print "The decrypted message is: ", output
- #=====================================#
- #===========MAIN PROGRAM==============#
- #=====================================#
- def main():
- menu(menus)
- choice = input("Please make a selection(1-3): ")
- #Encryption
- if choice == 1:
- enc(key_size, block_size)
- #Decryption
- elif choice == 2:
- dec(key_size, block_size)
- #Exit
- elif choice == 3:
- print "Exiting Program. Goodbye!"
- print
- print
- return
- #Invalid Choice
- else:
- print "ERROR. Invalid Selection. "
- print
- print
- return main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement