AES
Advanced Encryption Standard
AES-128/192/256
AES Interface

AES Internals



AES-CBC



AES-CBC-HMAC


AEAD

Lab
Reference
Last updated
Advanced Encryption Standard










Last updated
# AES-CBC encryption
>>> import json
>>> from base64 import b64encode
>>> from Crypto.Cipher import AES
>>> from Crypto.Util.Padding import pad
>>> from Crypto.Random import get_random_bytes
>>>
>>> data = b"secret"
>>> key = get_random_bytes(16)
>>> cipher = AES.new(key, AES.MODE_CBC)
>>> ct_bytes = cipher.encrypt(pad(data, AES.block_size))
>>> iv = b64encode(cipher.iv).decode('utf-8')
>>> ct = b64encode(ct_bytes).decode('utf-8')
>>> result = json.dumps({'iv':iv, 'ciphertext':ct})
>>> print(result)
'{"iv": "bWRHdzkzVDFJbWNBY0EwSmQ1UXFuQT09", "ciphertext": "VDdxQVo3TFFCbXIzcGpYa1lJbFFZQT09"}'# AES-CBC decryption
>>> import json
>>> from base64 import b64decode
>>> from Crypto.Cipher import AES
>>> from Crypto.Util.Padding import unpad
>>>
>>> # We assume that the key was securely shared beforehand
>>> try:
>>> b64 = json.loads(json_input)
>>> iv = b64decode(b64['iv'])
>>> ct = b64decode(b64['ciphertext'])
>>> cipher = AES.new(key, AES.MODE_CBC, iv)
>>> pt = unpad(cipher.decrypt(ct), AES.block_size)
>>> print("The message was: ", pt)
>>> except (ValueError, KeyError):
>>> print("Incorrect decryption")