Encrypt and decrypt using PyCrypto AES-256
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you need AES-256 in Python today, the practical answer is to use PyCryptodome, not the old unmaintained PyCrypto package. AES-256 means a 32-byte key, but secure encryption also requires the right mode, a unique nonce or IV, and integrity protection so decryption can detect tampering.
Use an Authenticated Mode
A common beginner mistake is to focus only on “AES-256” and ignore the mode of operation. Modern code should usually use an authenticated mode such as GCM rather than older patterns like ECB or CBC without a MAC.
With GCM, you get both encryption and integrity verification.
That encrypts the message and produces three values you must keep for decryption: the ciphertext, the authentication tag, and the nonce.
Decrypt and Verify
Decryption must verify the tag. If you skip verification, you lose one of the main security benefits of GCM.
If the ciphertext, tag, or nonce is wrong, decrypt_and_verify raises an exception instead of silently returning corrupted data.
If You Start From a Password
Passwords are not AES keys. If the input is a human password, derive a 32-byte key using a key-derivation function such as scrypt or PBKDF2.
Store the salt alongside the encrypted output. During decryption, the same password and stored salt reproduce the key.
Packaging the Encrypted Output
A simple pattern is to store all required values together.
Any format is fine as long as decryption has access to everything it needs. The key point is that nonce and tag are not optional metadata. They are required.
Why Not Legacy PyCrypto
The title may mention PyCrypto because many old answers do. In practice, the maintained drop-in replacement is PyCryptodome.
That gives you the Crypto namespace used in the examples above while avoiding an abandoned package.
What to Avoid
Avoid ECB mode entirely. It reveals patterns in the plaintext and is not acceptable for real security-sensitive use.
Also avoid reusing a nonce with the same key in GCM mode. Nonce reuse can break the security guarantees badly.
Finally, do not hardcode keys in source code or derive them by simply padding a password string to 32 bytes.
If you need key rotation, version your encrypted payload format so old ciphertext can still be decrypted safely during migration.
Common Pitfalls
The most common mistake is using AES without an authenticated mode or without a separate integrity check.
Another mistake is confusing “32-character password” with “32-byte cryptographic key.” Passwords should go through a derivation function first.
Developers also often forget to store the nonce or authentication tag, which makes correct decryption impossible.
Summary
- Use
PyCryptodome, not legacyPyCrypto, for modern Python AES work. - AES-256 requires a 32-byte key.
- Prefer authenticated modes such as GCM.
- If the source secret is a password, derive the key with
scryptor PBKDF2. - Store the nonce, ciphertext, tag, and salt together so decryption can succeed safely.
Related reading
- Encrypting the Hadoop Distributed Cache file
- Enforce MFA for AWS console login but not for API calls
- Error certificate signed by unknown authority after switching GCP project
- Error reading service account token from /var/run/secrets/kubernetes.io/serviceaccount/token. Ignoring
- Ensemble of different kinds of regressors using scikit-learn or any other python framework
- EOFError Compressed file ended before the end-of-stream marker was reached - MNIST data set
- Error response from daemon Get https//ghcr.io/v2/ denied denied
- Error when trying to obtain a certificate The specified item could not be found in the keychain

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.