AES (Advanced Encryption Standard) is a symmetric block cipher standardized by NIST. It has a fixed data block size of 16 bytes. Its keys can be 128, 192, or 256 bits long.
AES is implemented using the ESP32 hardware module.
from crypto import AES import crypto key = b'notsuchsecretkey' # 128 bit (16 bytes) key iv = crypto.getrandbits(128) # hardware generated random IV (never reuse it) cipher = AES(key, AES.MODE_CFB, iv) msg = iv + cipher.encrypt(b'Attack at dawn') # ... after properly sent the encrypted message somewhere ... cipher = AES(key, AES.MODE_CFB, msg[:16]) # on the decryption side original = cipher.decrypt(msg[16:]) print(original)
Create an AES object that will let you encrypt and decrypt messages.
The arguments are:
key
mode
AES.MODE_ECB
AES.MODE_CBC
AES.MODE_CFB
plaintext
ciphertext
segment_size
AES.MODE_CTR
IV
AES.MODE_CRT
To avoid security issues, IV should always be a random number and should never be reused to encrypt two different messages. The same applies to the counter in CTR mode. You can use crypto.getrandbits() for this purpose.
crypto.getrandbits()
counter
AES.SEGMENT_8
AES.SEGMENT_128
Encrypt data with the key and the parameters set at initialization.
Decrypt data with the key and the parameters set at initialization.