AES-CBC vs AES-GCM: Tampering, Nonces, and Legacy Compatibility
TL;DR
See how a modified CBC IV changes plaintext, how GCM rejects modified ciphertext, and how to read the CryptoJS passphrase format.
Try the Interactive Tool
Encrypt the same text twice with the AES tool and compare the salted CBC ciphertexts before reading the integrity discussion.
AES-CBC and AES-GCM can both encrypt bytes with an AES key, but they make different promises when somebody alters those bytes. CBC encryption alone hides the plaintext. It leaves the receiver without an authentication tag to check. GCM encrypts and authenticates the data together. The receiver can reject a modified message before using its plaintext. That distinction matters when encrypted values are stored in a database, shared through an untrusted channel, or returned to a server by a client.
DevStackTools' AES tool supports CryptoJS passphrase-based CBC for text. Use it to inspect or reproduce an existing salted CryptoJS payload. For a new application format, select an authenticated encryption library or API and document how the receiver obtains its key and nonce. A new encryption format also needs a plan for key rotation, migration and identifying which version produced each record.
What the DevStackTools output contains
Enter a public test message and passphrase in the AES tool, choose Encrypt, and press Run twice. Both results decrypt to the same text, but the Base64 strings differ because the tool generates a random salt on each run. In the serialized CryptoJS format, the decoded bytes start with the ASCII marker Salted__ followed by the salt and encrypted bytes. That marker often produces a Base64 prefix of U2FsdGVkX1. It identifies a container convention. It does not identify a verified sender.
CryptoJS passphrase mode derives a key and IV from the passphrase and salt using an OpenSSL-compatible derivation. This is different from supplying a raw AES key. A library that expects raw key bytes, a separate IV, and a ciphertext buffer cannot decrypt the whole Base64 string directly. Interoperability requires matching the passphrase encoding, derivation settings, CBC mode, padding and envelope format. Passing the same human-readable password to two libraries can still give incompatible results if one uses a different derivation.
The default CryptoJS passphrase derivation is fast and uses MD5 with one iteration. An attacker with the ciphertext can attempt guesses offline. A random salt prevents the same passphrase from deriving the same values for every message, but it does not make weak guesses expensive. In a new password-based design, use a password KDF with a deliberately chosen cost, then use the derived key with authenticated encryption. Store the KDF parameters and salt in the record so a later reader can reproduce the derivation. Keep the salt distinct from the encryption nonce. Each has its own job.
Example 1: Change the first plaintext byte in CBC
The following Node.js example uses a raw key and IV so the effect is visible. Both are fixed demonstration values. In CBC, the first decrypted block is XORed with the IV. Changing one bit in the IV flips the same bit in that plaintext block. Here the change converts an uppercase A to an uppercase B. The ciphertext stays identical and decryption still succeeds.
Worked example
const { createCipheriv, createDecipheriv } = require('node:crypto');
const key = Buffer.alloc(32);
const iv = Buffer.alloc(16);
const cipher = createCipheriv('aes-256-cbc', key, iv);
const ciphertext = Buffer.concat([cipher.update('Amount=100'), cipher.final()]);
const modifiedIV = Buffer.from(iv);
modifiedIV[0] ^= 'A'.charCodeAt(0) ^ 'B'.charCodeAt(0);
const decipher = createDecipheriv('aes-256-cbc', key, modifiedIV);
console.log(Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString());
Bmount=100
The DevStackTools envelope derives the IV from the salt instead of carrying the separate IV shown in this example. The underlying lesson is the same: CBC supplies no tag to authenticate the message. An incorrect password or altered payload can cause bad padding or invalid UTF-8. A successful decode does not establish integrity either. If a legacy integration already authenticates the envelope with a separate MAC, verify how the MAC is calculated and when it is checked. That authentication must cover the metadata needed for safe decryption too.
Example 2: Modify ciphertext and check the GCM tag
This Node.js example authenticates both the ciphertext and a record label. The label is additional authenticated data, often called AAD. It is available to the receiver and does not need to be secret. Use AAD for fields such as a stable record type or format version when swapping messages between those contexts must fail.
const { createCipheriv, createDecipheriv, randomBytes } = require('node:crypto');
const key = randomBytes(32);
const nonce = randomBytes(12);
const label = Buffer.from('invoice:v1');
const cipher = createCipheriv('aes-256-gcm', key, nonce);
cipher.setAAD(label);
const ciphertext = Buffer.concat([cipher.update('Amount=100'), cipher.final()]);
const tag = cipher.getAuthTag();
function read(bytes) {
const decipher = createDecipheriv('aes-256-gcm', key, nonce);
decipher.setAAD(label);
decipher.setAuthTag(tag);
return Buffer.concat([decipher.update(bytes), decipher.final()]).toString();
}
console.log(read(ciphertext));
const altered = Buffer.from(ciphertext);
altered[0] ^= 1;
try {
console.log(read(altered));
} catch {
console.log('Rejected modified ciphertext');
}
Amount=100
Rejected modified ciphertext
The reader releases the string only after final verifies the tag. Streaming code needs the same discipline. A buffer returned earlier by update may contain plaintext before the authentication result is known. Keep it out of parsers, application state and downstream streams until the check succeeds. Handle errors consistently so callers cannot learn details about partially decrypted data.
Plan for nonce uniqueness and migration
A GCM nonce must be unique under the same key. Reusing it can expose relationships between messages and undermine authentication. A fresh nonce needs to be created at the encryption boundary every time a message is encrypted, including when a write is retried and when multiple processes encrypt concurrently. If using random nonces, account for the volume of encryptions under each key. For a counter-based scheme, make sure parallel writers, restarts and restored backups cannot repeat a value under the active key. Record the nonce with the ciphertext and authentication tag.
When migrating existing data, use an explicit envelope version so a reader can distinguish legacy CBC records from new GCM records. Validate the legacy record in its original application context before re-encrypting it, because a missing legacy authentication tag cannot be supplied retroactively. New records can carry a key identifier, nonce, ciphertext and tag, plus any KDF metadata required for password-based encryption. Make key access, rotation and retirement separate from how the envelope is encoded.
For a public compatibility test, encrypt and decrypt a sample in AES Encrypt / Decrypt and inspect the salted Base64 result. If your task is verifying a login password rather than recovering encrypted text, start with the bcrypt password hashing guide.