AES is a block cipher: it encrypts exactly 16 bytes at a time. Real messages are longer than 16 bytes, so you need a mode of operation that defines how blocks are chained together. The mode you pick matters as much as the cipher itself. Two of the most common are AES-CBC and AES-GCM, and they differ in one crucial way: only one of them tells you when the ciphertext has been tampered with.
This guide explains how each works, where each fails, and which one to choose.
The short answer
Use AES-GCM (or ChaCha20-Poly1305) unless you have a legacy reason not to. GCM is an authenticated mode: it encrypts and produces an authentication tag in a single pass. CBC only encrypts, so you must add a separate MAC and verify it correctly, and many real-world systems have gotten that wrong.
How AES-CBC works
In Cipher Block Chaining, each plaintext block is XORed with the previous ciphertext block before being encrypted. The first block is XORed with a random initialization vector (IV).
C0 = IV
Ci = AES_encrypt(key, Pi XOR C(i-1))Key properties:
- IV must be unpredictable and random for every message. A predictable IV enables chosen-plaintext attacks (the basis of the BEAST attack on TLS 1.0).
- Padding is required. Plaintext must be a multiple of 16 bytes, so PKCS#7 padding is added. Handling padding errors is where things go wrong.
- No integrity protection. Anyone who can modify the ciphertext can flip bits in the next plaintext block in a controlled way. The decryptor has no built-in way to notice.
The padding oracle problem
If a server decrypts CBC data and responds differently for "bad padding" and "bad content", an attacker can decrypt a message byte by byte without knowing the key. This is a padding oracle attack, and it has broken ASP.NET, Java frameworks, and several TLS implementations (POODLE, Lucky13).
The fix is encrypt-then-MAC: compute an HMAC over the IV and ciphertext, and verify it in constant time before you attempt to decrypt. It works, but it is easy to get wrong.
How AES-GCM works
Galois/Counter Mode combines two things:
- CTR mode encryption. AES encrypts an incrementing counter to produce a keystream, which is XORed with the plaintext. No padding is needed, and ciphertext length equals plaintext length.
- GHASH authentication. A polynomial hash over the ciphertext (and optional associated data) produces a 16-byte authentication tag.
On decryption, the tag is verified first. If a single bit has changed, decryption fails and returns no plaintext.
This is AEAD: Authenticated Encryption with Associated Data. The associated data (AAD) is authenticated but not encrypted, which is useful for headers, user IDs, or versions that must stay readable but must not be altered.
Side-by-side comparison
| AES-CBC | AES-GCM | |
|---|---|---|
| Confidentiality | Yes | Yes |
| Integrity / authenticity | No (needs separate HMAC) | Yes, built in |
| Padding | Required (PKCS#7) | None |
| IV / nonce size | 16 bytes, random | 12 bytes (96-bit) recommended |
| Parallel encryption | No | Yes |
| Parallel decryption | Yes | Yes |
| Hardware acceleration | AES-NI | AES-NI + PCLMULQDQ |
| Biggest pitfall | Padding oracles, unauthenticated data | Nonce reuse |
The GCM caveat: never reuse a nonce
GCM is safer by default, but it has one sharp edge. Reusing a (key, nonce) pair is catastrophic. With two ciphertexts encrypted under the same key and nonce, an attacker can XOR them to cancel the keystream, revealing the XOR of the plaintexts. Worse, they can recover the GHASH authentication key and forge valid tags for arbitrary messages.
How to avoid it:
- Random 96-bit nonces are fine up to about 2^32 messages per key (NIST guidance). Past that, the birthday bound makes collisions plausible.
- Counter-based nonces are collision-free but require reliable state, which is hard across multiple servers or after a crash and restore.
- Rotate keys before you approach the message limit, or use a nonce-misuse-resistant mode such as AES-GCM-SIV if you cannot guarantee uniqueness.
Never use a fixed nonce or one derived from a timestamp.
Practical example with Web Crypto
async function encrypt(key, plaintext) {
const iv = crypto.getRandomValues(new Uint8Array(12)); // fresh 96-bit nonce
const data = new TextEncoder().encode(plaintext);
const ciphertext = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv, additionalData: new TextEncoder().encode('user:42') },
key,
data
);
// Store iv alongside ciphertext. The 16-byte tag is appended automatically.
return { iv, ciphertext: new Uint8Array(ciphertext) };
}The IV is not secret, so you store or transmit it with the ciphertext. If decrypt throws, treat the data as tampered with and discard it. Do not try to "recover" a partial result.
Choosing in practice
- New application code: AES-256-GCM (or ChaCha20-Poly1305 on devices without AES hardware).
- Encrypting data at rest with a database or file format: AES-GCM, binding the record ID as AAD so ciphertexts cannot be swapped between rows.
- Legacy protocols or compliance requirements that mandate CBC: use encrypt-then-MAC with HMAC-SHA-256, separate keys for encryption and MAC, and a constant-time tag comparison.
- Disk encryption: neither. Use XTS, which is designed for storage sectors.
Key takeaways
- Encryption without authentication is not enough. Attackers can modify ciphertext, and with CBC they can sometimes decrypt it too.
- GCM gives you confidentiality and integrity in one primitive, with no padding.
- GCM's one hard rule is never reuse a nonce with the same key.
- Generate keys and IVs from a cryptographically secure random source, never
Math.random().
Want to experiment? Use the AES Encryption tool to encrypt and decrypt text with AES-GCM and AES-CBC right in your browser, and see how the IV, ciphertext, and tag fit together. Everything runs locally, so your keys and data never leave your device.