PyAES: Pure Python AES Implementation

1.6.1 · active · verified Fri Apr 10

PyAES provides a pure-Python implementation of the Advanced Encryption Standard (AES) block cipher, along with common modes of operation like CBC, CTR, and OFB. Currently at version 1.6.1, it offers a lightweight cryptographic option for environments where C extensions are not feasible or desired. Its release cadence is low, with updates typically addressing bug fixes or minor enhancements.

Warnings

Install

Imports

Quickstart

This example demonstrates encrypting and decrypting data using AES in CBC mode with a 256-bit key. It highlights the use of `AESModeOfOperationCBC` and the necessity of providing a unique IV for each encryption operation. PyAES uses zero-padding by default if the plaintext length is not a multiple of the block size.

import pyaes
import os

# AES in CBC mode (with default zero-padding)
key = os.urandom(32) # 256-bit key
iv = os.urandom(16)  # 128-bit Initialization Vector

aes_cbc_encrypt = pyaes.AESModeOfOperationCBC(key, iv=iv)
plaintext = b"This is some plaintext to encrypt. It will be zero-padded if not a multiple of 16 bytes."
ciphertext = aes_cbc_encrypt.encrypt(plaintext)

# Decrypt using a new AES object with the same key and IV
aes_cbc_decrypt = pyaes.AESModeOfOperationCBC(key, iv=iv)
decrypted_text = aes_cbc_decrypt.decrypt(ciphertext)

print(f"Original: {plaintext}")
print(f"Encrypted: {ciphertext.hex()}")
print(f"Decrypted: {decrypted_text}")

assert decrypted_text == plaintext

view raw JSON →