# MicroPython + AES256 on ESP32
# Docs: https://docs.micropython.org/en/latest/library/ucryptolib.html
import uos
from ucryptolib import aes
key = b'[Secret Wokwi key with 256 bits]'
iv = b'secret-iv-123456' # In real life, uos.urandom(16)
MODE_CBC = 2
cipher = aes(key, MODE_CBC, iv)
plain = "This text is a very top secret!"
print('Input: {}'.format(plain))
print("Using AES{}-CBC cipher".format(len(key * 8)))
# AES works in 16-bytes blocks, so we must pad the input text
padded = plain + " " * (16 - len(plain) % 16)
encrypted = cipher.encrypt(padded)
print('Encrypted: {}'.format(encrypted))
decipher = aes(key, MODE_CBC, iv)
decrypted = decipher.decrypt(encrypted)
print('Decrypted: {}'.format(decrypted.strip()))
print('')