How AES-GCM Encryption Works: Keys, IVs, Tags and Passphrases

Understand AES-256-GCM in plain terms: keys vs passphrases, why every message needs a fresh IV, what the auth tag does, and how to decrypt it in Node.js.

Updated September 29, 2026 7 min read

Use the free AES Encryption & DecryptionEncrypt and decrypt text with AES-256-GCM using a passphrase or raw key

You need to hand a database password to a contractor, park recovery codes in a shared notes app, or check that your own service decrypts data correctly. "Just use AES" is the usual answer, but AES on its own is only a building block. Whether the result is actually secure depends on the mode, on how the key is produced, and on how the random values around it are handled.

This guide explains what AES-GCM does, why passphrases need a key derivation step, what goes wrong when an IV is reused, and how to encrypt and decrypt with our browser tool and with a few lines of Node.js.

The pieces of an AES-GCM message

AES is a block cipher: it scrambles exactly 16 bytes at a time using a secret key of 128, 192 or 256 bits. To encrypt a message of any length you need a mode of operation, and the mode decides most of the security properties.

Piece Size Secret? Purpose
Key 16, 24 or 32 bytes Yes The only thing that must stay secret
IV (nonce) 12 bytes No Makes each encryption unique; must never repeat with the same key
Ciphertext Same length as the text No The encrypted data
Authentication tag 16 bytes No Proves the ciphertext and key are correct
Salt (passphrase mode) 16 bytes No Makes the derived key unique per message

GCM (Galois/Counter Mode) turns AES into a stream of key-dependent random bytes that is combined with your text, and it computes a tag over the result. On decryption the tag is recomputed. If one bit of the ciphertext was changed, or the wrong key was used, the tags differ and decryption fails outright. That is what "authenticated encryption" means, and it is why GCM is preferred over older modes.

Why not ECB or plain CBC?

ECB encrypts identical blocks to identical output, so patterns in the data leak through. CBC hides patterns but has no built-in integrity check, so an attacker can flip bits in the ciphertext and, in some systems, learn about the plaintext from error messages (padding oracle attacks). CBC can be made safe with a separate HMAC, but GCM gives you both properties in one step. For new designs, choose an authenticated mode such as AES-GCM.

The rule you must not break: never reuse an IV with the same key

GCM's IV does not need to be secret or unpredictable, but it must be unique for every message encrypted under the same key. If two messages share a key and IV, an attacker can combine the two ciphertexts to cancel out the keystream and can also forge valid tags. The practical rule is simple: generate a fresh random 12-byte IV for every encryption and store it next to the ciphertext.

Passphrases are not keys

A key is 32 random bytes. A passphrase is something a human typed, and it is far easier to guess. A key derivation function (KDF) bridges the gap. PBKDF2, scrypt and Argon2 all take the passphrase plus a random salt and deliberately do a lot of work, so each guess costs an attacker real time.

Two consequences matter in practice:

  • The passphrase is the weak point. AES-256 cannot be brute-forced, but "summer2024" can. Use a long random passphrase from a Passphrase Generator or Password Generator.
  • Share the passphrase over a different channel. Email the ciphertext, then read the passphrase out on a call or send it by a separate messenger.

Encrypt and decrypt text with our tool

The AES Encryption & Decryption tool uses AES-256-GCM through your browser's Web Crypto API, so the text and passphrase never leave the page.

  1. Choose Encrypt and then Passphrase (or Raw key if you already have a key).
  2. Type or paste your text into Plain text.
  3. Enter the passphrase. Leave PBKDF2 iterations at 600,000 unless you have a reason to change it; 210,000 is faster and 1,000,000 is slower to guess.
  4. Click Encrypt and copy or download the Encrypted (Base64) output. Encrypting the same text twice gives different results because a new salt and IV are generated each time.
  5. To reverse it, switch to Decrypt, paste the Base64 string, enter the same passphrase and click Decrypt. A wrong passphrase or an altered string produces an error rather than garbled text.

In Raw key mode, pick a New key size and click New key to create a random 128, 192 or 256-bit key, shown in hex. Keep that key somewhere safe; without it the data is gone. If you need a key in another encoding for your application, the Secret Key Generator can produce hex or Base64 values.

Decrypting the output in your own code

Open Output format on the tool page to see the byte layout. In passphrase mode (version 1) it is: one version byte 0x01, the iteration count as a 4-byte big-endian number, a 16-byte salt, a 12-byte IV, then the ciphertext with the 16-byte GCM tag appended. This Node.js function reads it:

const crypto = require("node:crypto");

// 0x01 | iterations (uint32 BE) | salt (16) | IV (12) | ciphertext + tag (16)
function decryptV1(b64, passphrase) {
  const buf = Buffer.from(b64, "base64");
  if (buf[0] !== 1) throw new Error("Not a v1 envelope");
  const iterations = buf.readUInt32BE(1);
  const salt = buf.subarray(5, 21);
  const iv = buf.subarray(21, 33);
  const tag = buf.subarray(buf.length - 16);
  const ct = buf.subarray(33, buf.length - 16);
  const key = crypto.pbkdf2Sync(Buffer.from(passphrase, "utf8"), salt, iterations, 32, "sha256");
  const d = crypto.createDecipheriv("aes-256-gcm", key, iv);
  d.setAuthTag(tag);
  return Buffer.concat([d.update(ct), d.final()]).toString("utf8");
}

If you are writing your own encryption with a raw 32-byte key, the same pattern applies in reverse. Note the fresh IV on every call:

const crypto = require("node:crypto");

function encrypt(plaintext, key) {
  const iv = crypto.randomBytes(12); // new random IV for every message
  const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
  const ct = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
  return Buffer.concat([iv, ct, cipher.getAuthTag()]).toString("base64");
}

function decrypt(b64, key) {
  const buf = Buffer.from(b64, "base64");
  const iv = buf.subarray(0, 12);
  const tag = buf.subarray(buf.length - 16);
  const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
  decipher.setAuthTag(tag);
  return Buffer.concat([decipher.update(buf.subarray(12, buf.length - 16)), decipher.final()]).toString("utf8");
}

In a real application, load the key from an environment variable or secrets manager, never from source code. A vetted high-level library, such as libsodium bindings, is usually safer than assembling primitives yourself.

Other ways to encrypt text

  • Password managers with secure sharing are the best option for passing credentials between colleagues, because nothing needs to be copied around.
  • age and GnuPG encrypt files and text from the command line, with passphrases or public keys, and are well suited to long-term storage.
  • Archive tools such as 7-Zip can create AES-encrypted archives when you need to send whole files.
  • openssl enc exists, but its command-line interface does not support GCM, and older examples often use weak key derivation. It is easy to misuse, so prefer the options above.

Pitfalls to avoid

Mixing up encoding and encryption

Base64 only changes how bytes are written; anyone can reverse it with a Base64 converter. The ciphertext is Base64-encoded for convenience, but the protection comes from AES and the key.

Expecting interoperability between random AES tools

There is no universal "AES text" format. Tools differ in mode, KDF, iteration count, IV placement and encoding, so a string from one site generally cannot be decrypted by another. Document the format you use.

Confusing encryption with hashing

Encryption is reversible with the key. A hash, such as one from the Hash Generator, is one-way and is used for fingerprints and integrity checks, not for hiding data you need back.

Frequently asked questions

Is AES-256 more secure than AES-128?

Both are considered secure against brute force today. AES-256 offers a larger safety margin, and the practical risk almost always lies in weak passphrases, reused IVs or leaked keys rather than the key length.

What happens if I reuse an IV in AES-GCM?

Reusing an IV with the same key lets an attacker recover the XOR of the two plaintexts and forge authentication tags. Always generate a new random 12-byte IV for each message.

Do I need to keep the IV and salt secret?

No. They are stored alongside the ciphertext and are needed for decryption. Only the key or passphrase must remain secret.

Why does decryption fail instead of giving wrong text?

GCM checks the authentication tag before returning anything. A wrong key or passphrase, or any change to the ciphertext, makes the tag check fail, which protects you from silently using corrupted data.

Can I recover data if I lose the passphrase?

No. There is no backdoor. Keep the passphrase or key in a password manager or another secure place before you delete the original text.

Try it now: AES Encryption & Decryption

Loading tool…