From charlesreid1

from Crypto.Cipher import AES
from Crypto.Util import RFC1751
from Crypto import Random


AES_MODE = AES.MODE_CBC


def encrypt(text, key):
    text += '\0' * (16 - (len(text) % 16))
    IV = Random.new().read(AES.block_size)
    crypto = AES.new(key, AES_MODE, IV=IV)
    return IV + crypto.encrypt(text)


def decrypt(ciphertext, key):
    crypto = AES.new(key, AES_MODE, IV=ciphertext[:AES.block_size])
    plain = crypto.decrypt(ciphertext[AES.block_size:])

    return plain.rstrip('\0')


if __name__ == '__main__':
    key = Random.new().read(32)
    text = "This is a test"

    ciphertext = encrypt(text, key)
    print("Plaintext was '%s'" % decrypt(ciphertext, key))
    print("Key was '%s'" % RFC1751.key_to_english(key))

https://www.reddit.com/r/cryptography/comments/4yu7mh/code_review_of_aes_implementation_in_python/