# ========================================================= # For more information: www.blog.pythonlibrary.org/ # 2016/05/18/ python-3-an-intro-to-encryption/ # # Documentation: pythonhosted.org/pycrypto/ # toc-Crypto.Cipher.AES-module.html # # Note: AES message must be a multiple of 16 # ========================================================= from Crypto.Cipher import AES # --------------------------------------------------------- # --- global variables # --------------------------------------------------------- key = 'This is a key123' msg = 'The answer is maybe, not yes or no' # --------------------------------------------------------- # --- functions # --------------------------------------------------------- # --- pad string to a multiple of 'size' def pad(size,text): while len(text) % size != 0: text += ' ' return text # --------------------------------------------------------- # -- main # --------------------------------------------------------- # --------------------------------------------------------- print('--- encrypt --------------------------------------') # --------------------------------------------------------- aes = AES.new(key, AES.MODE_CBC, 'This is an IV456') ciphertext = aes.encrypt(pad(16,msg)) print('AES ciphertext is {}'.format(type(ciphertext))) print('AES ciphertext = {}'.format(ciphertext)) # --------------------------------------------------------- print('--- decrypt --------------------------------------') # --------------------------------------------------------- aes2 = AES.new(key, AES.MODE_CBC, 'This is an IV456') msg2 = aes2.decrypt(ciphertext) print('AES msg2 is {}'.format(type(msg2))) print('AES msg2 = {}'.format(msg2))