AES in ECB mode
Electronic Codebook mode encrypts each 16-byte block independently, so identical plaintext blocks produce identical ciphertext blocks.
Why it's at risk
ECB leaks the structure of the data (the famous "ECB penguin" image) and provides no integrity, so attackers can reorder or replay blocks. This is a classical design flaw that no key size fixes. Use an authenticated mode such as AES-GCM or ChaCha20-Poly1305.
- Used for
- Symmetric encryption
- Family
- Block cipher mode
- Standards
- NIST SP 800-38A
Migration guides
Concrete code changes to move off AES in ECB mode. Pick your stack.
node:crypto · javascriptNode.js: replace DES/3DES/RC4/Blowfish/ECB (and AES-128 for long-lived data) with AES-256-GCMProduction-ready
Why
DES (56-bit key) can be brute-forced; Triple DES and Blowfish have 64-bit blocks that leak data after a few tens of gigabytes (Sweet32); RC4 output is biased; ECB mode reveals patterns and has no integrity. None of these is a quantum issue: they are weak today. The fix is an authenticated cipher: AES-256-GCM or ChaCha20-Poly1305. AES-128-GCM is still acceptable (NIST treats AES-128 as post-quantum Security Category 1), but AES-256 adds margin for data that must stay secret for decades and is required by CNSA 2.0.
Before
import { createCipheriv } from 'node:crypto';
const c1 = createCipheriv('des-ede3-cbc', key24, iv8); // 3DES
const c2 = createCipheriv('aes-128-ecb', key16, null); // ECB: patterns + no integrity
const c3 = createCipheriv('bf-cbc', key, iv8); // BlowfishAfter
import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';
function encrypt(key32, plaintext, aad = Buffer.alloc(0)) {
const iv = randomBytes(12); // unique per encryption with the same key
const c = createCipheriv('aes-256-gcm', key32, iv);
c.setAAD(aad);
const ct = Buffer.concat([c.update(plaintext), c.final()]);
return Buffer.concat([Buffer.from([1]), iv, c.getAuthTag(), ct]); // version byte | iv | tag | ct
}
function decrypt(key32, blob, aad = Buffer.alloc(0)) {
if (blob[0] !== 1) throw new Error('unknown format version');
const iv = blob.subarray(1, 13), tag = blob.subarray(13, 29), ct = blob.subarray(29);
const d = createDecipheriv('aes-256-gcm', key32, iv);
d.setAAD(aad);
d.setAuthTag(tag);
return Buffer.concat([d.update(ct), d.final()]); // throws if tampered
}'chacha20-poly1305' (with authTagLength: 16) is a good alternative on CPUs without AES acceleration.
Gotchas
- Never reuse an IV/nonce with the same GCM key. Random 96-bit IVs are fine up to about 2^32 messages per key; rotate keys before that, or use a counter.
- Keys must be random 32-byte values, not passwords. Derive from passwords with scrypt/Argon2, and from shared secrets with HKDF.
- Existing data: keep a decrypt-only path for the old format (the version byte makes this easy), re-encrypt on read or in a batch job, then delete the old path.
- Tag check: always call
setAuthTagbeforefinal(); skipping it removes the integrity guarantee. - Legacy algorithms may be unavailable: OpenSSL 3 moves DES, Blowfish and RC4 to the "legacy" provider, so old code may already fail on new Node versions.
Verify
Round-trip test, a tamper test (flip one byte, expect an exception), then npx -y pqe-cli scan . to confirm no des, 3des, rc4, blowfish or aes-ecb findings remain.
cryptography (pyca) AEAD · pythonPython: replace DES/3DES/RC4/Blowfish/ECB with AES-256-GCM or ChaCha20-Poly1305Production-ready
Why
These ciphers and modes are weak against today's computers (small keys, 64-bit blocks, biased keystreams, or no integrity). Use an AEAD from pyca/cryptography, which handles the mode and authentication correctly.
Before
from Crypto.Cipher import DES3, AES # PyCryptodome
c1 = DES3.new(key24, DES3.MODE_CBC, iv8)
c2 = AES.new(key16, AES.MODE_ECB) # patterns leak, no integrityAfter
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM, ChaCha20Poly1305
key = AESGCM.generate_key(bit_length=256) # store in a KMS / secret manager
aead = AESGCM(key) # or ChaCha20Poly1305(ChaCha20Poly1305.generate_key())
nonce = os.urandom(12) # never reuse with the same key
blob = b"\x01" + nonce + aead.encrypt(nonce, plaintext, aad) # version | nonce | ct+tag
def decrypt(blob: bytes, aad: bytes) -> bytes:
assert blob[0] == 1, "unknown format"
return aead.decrypt(blob[1:13], blob[13:], aad) # raises InvalidTag if tamperedGotchas
- Nonce reuse with AES-GCM is catastrophic; random 96-bit nonces are fine for up to roughly 2^32 messages per key.
AESGCMSIV(pyca 42+) tolerates accidental reuse better. - Do not hand-roll CBC + HMAC unless you must interoperate; if you must, encrypt-then-MAC with separate keys.
- Password-derived keys need scrypt/Argon2 (
cryptography.hazmat.primitives.kdf.scrypt.Scrypt), not a hash of the password. - Old data: keep a decrypt-only path for the legacy format, re-encrypt, then remove it.
- PyCryptodome offers
AES.MODE_GCMtoo if you can't switch libraries; the same nonce rules apply.
Verify
Round-trip and tamper tests, then npx -y pqe-cli scan ..
JCA (javax.crypto / java.security) · javaJava: fix Cipher.getInstance("AES") (it means ECB), DES/3DES, MD5 and SHA-1Production-ready
Why
The most common Java crypto bug is Cipher.getInstance("AES"): with the default SunJCE provider that means AES/ECB/PKCS5Padding, which leaks patterns and has no integrity. DES/3DES/RC4/Blowfish and MD5/SHA-1 are weak against ordinary computers. Fixing these is quick and independent of the post-quantum work.
Before
Cipher c = Cipher.getInstance("AES"); // = AES/ECB/PKCS5Padding
Cipher d = Cipher.getInstance("DESede/CBC/PKCS5Padding");
MessageDigest md = MessageDigest.getInstance("MD5");After
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import java.security.*;
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(256);
SecretKey key = kg.generateKey();
byte[] iv = new byte[12];
SecureRandom.getInstanceStrong().nextBytes(iv); // or new SecureRandom()
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(128, iv));
c.updateAAD(aad);
byte[] ct = c.doFinal(plaintext); // ciphertext || 16-byte tag
// store iv + ct (+ a format version byte)
MessageDigest sha = MessageDigest.getInstance("SHA-256");
Mac mac = Mac.getInstance("HmacSHA256");Gotchas
- Always spell out the full transformation (
algorithm/mode/padding); provider defaults differ. - GCM IVs must be unique per key; the JDK refuses to reuse the same key+IV pair on one encrypting
Cipherinstance, but cannot protect you across instances. getInstanceStrong()may block on some Linux configurations;new SecureRandom()is fine for IVs and keys.- Passwords: use a dedicated password hashing library (Argon2id, e.g. via Bouncy Castle or Spring Security's
Argon2PasswordEncoder), notMessageDigest. - Legacy data: keep a decrypt-only path for old formats, then re-encrypt.
Verify
Round-trip test plus tamper test (AEADBadTagException expected), then npx -y pqe-cli scan ..