def encrypt(plaintext, password): salt = Crypto.Random.get_random_bytes(SALT_SIZE) key = generate_key(password, salt, NUMBER_OF_ITERATIONS) cipher = AES.new(key, AES.MODE_ECB) #PG: Consider changing to MODE_CBC padded_plaintext = pad_text(plaintext, AES_MULTIPLE) ciphertext = cipher.encrypt(padded_plaintext) ciphertext_with_salt = salt + ciphertext return ciphertext_with_salt
def decrypt(ciphertext, password): salt = ciphertext[0:SALT_SIZE] #iv = ciphertext[:AES.block_size] ciphertext_sans_salt = ciphertext[SALT_SIZE:] key = generate_key(password, salt, NUMBER_OF_ITERATIONS) cipher = AES.new(key, AES.MODE_ECB) #PG: Consider changing to MODE_CBC padded_plaintext = cipher.decrypt(ciphertext_sans_salt) plaintext = unpad_text(padded_plaintext) return plaintext