My name is Pat, I'm a rising high school senior interested in Comp Sci. I've taken interest in cryptography following the NSA scandal. Since this is the first time I've ever implemented encryption my projects, I was wondering if I'm doing anything wrong!
Thanks so much in advance!
import sys import os from Crypto.Cipher import AES from Crypto.Protocol.KDF import PBKDF2 from Crypto import Random import base64 class simpleAES(object): def encrypt(self, data = '', key = '', key_size = 32, iterations = 1000): # ENCRYPT salt = Random.new().read(key_size) #salt the hash iv = Random.new().read(AES.block_size) pre_key = PBKDF2(key, salt, key_size, iterations) derived_key = PBKDF2(key, pre_key) cipher = AES.new(derived_key, AES.MODE_CFB, iv) encodedtext = iv + cipher.encrypt(data) encodedtext = base64.b64encode(encodedtext) pre_key = base64.b64encode(pre_key) derived_key = base64.b64encode(derived_key) # END ENCRYPT return encodedtext + "$" + pre_key def decrypt(self, data = '', key = ''): # DECRYPT pre_key = data.split('$') data = data.split('$') data = base64.b64decode(data) iv = '0000000000000000' pre_key = base64.b64decode(pre_key) decoded_key = PBKDF2(key, pre_key) cipher = AES.new(decoded_key, AES.MODE_CFB, iv) decodedtext = str(cipher.decrypt(data))[16:] #remove iv # END DECRYPT return decodedtext aes = simpleAES() enc = aes.encrypt("hello world this is some text i want to encrypt", "password1") print enc print print aes.decrypt(enc, "password1")
Any advice is greatly appreciated! Thank you for your time!