Registry / auth-security / pyaes
library1.6.1pypypi✓ verified 26d ago

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.

pip install pyaes
INSTALL
IMPORT
SIG · PYAES
P
pyaes
auth-securitypythonv1.6.1
Install
2.4s avg
Import
10ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 19.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AESModeOfOperationCBC
from pyaes import AESModeOfOperationCBC
AESModeOfOperationCTR
from pyaes import AESModeOfOperationCTR
AES
from pyaes import AES
Use this for the raw block cipher; typically prefer mode-of-operation classes for common use cases.

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
Debug
Known issues
gotchaAES key lengths must be 16, 24, or 32 bytes (for AES-128, AES-192, AES-256 respectively). Initialization Vector (IV) for CBC mode must be 16 bytes. Providing incorrect lengths will result in errors or insecure operation.
fix
Ensure `key` is 16, 24, or 32 bytes long, and `iv` (for CBC/CTR/OFB) is 16 bytes long. Use `os.urandom()` for cryptographically secure random values.
affects: All versions
breakingThe stream API (e.g., `encrypt_stream`, `decrypt_stream`) was broken in version 1.5.0 and fixed in 1.6.0. Users upgrading from 1.5.0 to 1.6.0 will find their stream operations now function correctly, which might be a 'breaking change' if their code had workarounds for the 1.5.0 bug.
fix
Ensure you are using version 1.6.0 or higher for reliable stream API functionality. Review code that interacted with the stream API in 1.5.0 as workarounds may no longer be necessary or compatible.
affects: 1.5.0 (bugged), 1.6.0+
gotchaPyAES uses simple zero-padding by default when input data is not a multiple of the AES block size (16 bytes). This can lead to security vulnerabilities (e.g., padding oracle attacks) or data loss if the original plaintext legitimately ends with null bytes, as zero-padding cannot distinguish between actual nulls and padding nulls.
fix
For robust applications, implement a cryptographically secure padding scheme like PKCS7 or ISO 10126 before encryption and unpadding after decryption, rather than relying on `pyaes`'s default zero-padding.
affects: All versions
gotchaReusing the same Initialization Vector (IV) with the same key for multiple encryptions in CBC mode significantly compromises confidentiality, making the encryption vulnerable to attacks. A unique, unpredictable IV is crucial for each encryption operation.
fix
Always generate a new, cryptographically random IV (e.g., `os.urandom(16)`) for every distinct encryption operation with the same key in CBC mode. Store and transmit the IV alongside the ciphertext (it does not need to be secret).
affects: All versions (when using CBC mode)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyaes'
The 'pyaes' library is not installed in the current Python environment or is not accessible within the Python path.
fix
Install the library using pip: `pip install pyaes`
ValueError: Invalid key size
The AES key provided for encryption or decryption does not meet the required length of 16, 24, or 32 bytes.
fix
Ensure the key is exactly 16, 24, or 32 bytes long. Example: `key = b'This is a 16-byte key'`
ValueError: plaintext block must be 16 bytes
When using `AESModeOfOperationCBC` (or other block modes) directly, the `encrypt` method expects plaintext to be an exact multiple of the AES block size (16 bytes); for arbitrary length data, `pyaes.BlockFeeder` should be used.
fix
Utilize `pyaes.Encrypter` and `pyaes.Decrypter` with `feed` and `feed_final` methods to handle plaintext of any length, as these classes include automatic padding. Example: `encrypter = pyaes.Encrypter(aes_mode); ciphertext = encrypter.feed(plaintext); ciphertext += encrypter.feed_final()`
TypeError: a bytes-like object is required, not 'str'
Cryptographic functions in `pyaes` (and Python 3 generally for bytes-oriented operations) expect byte strings (e.g., `b'hello'`) rather than regular Unicode strings (`'hello'`).
fix
Encode your string data to bytes before passing it to `pyaes` functions. Example: `plaintext.encode('utf-8')` or `key_string.encode('utf-8')`
Upgrade
Version history
1.6.1latest on PyPI · released Sep 20, 2017
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
OpenAI (training)
1
Resources