Menu

AES-GCM in TypeScript: the 12-byte nonce, the tag, and why your ciphertext won't decrypt on the other side
📰

AES-GCM in TypeScript: the 12-byte nonce, the tag, and why your ciphertext won't decrypt on the other side

DEV Community: security·Alejandro Rodriguez·6 days ago
#qyywJVJT
#devtosecurity#devto#dev#nonce#encloom
Reading 0:00
15s threshold

Your Node backend encrypts with AES-256-GCM, the frontend decrypts with Web Crypto, the key and IV match… and crypto.subtle.decrypt throws OperationError . The key isn't missing. The tag is. Node hands it to you separately via getAuthTag() , and Web Crypto expects it appended to the end of the ciphertext.

This post continues the thread of the last two: Buffer vs Uint8Array in TypeScript crypto , where AES-GCM showed up in passing, and HMAC over JSON in TypeScript , where the problem was different bytes under the MAC. Here the problem is bytes that are missing, extra, or repeated. The examples use encloom 2.0.4 ( encloom/aes-gcm ), but the format is the same as Web Crypto's, so everything applies if you do it by hand too.

Not a compliance guide. About bytes on the wire.

What AES-GCM actually returns

GCM encrypts and authenticates in one pass. It takes a key (16, 24, or 32 bytes), a nonce (also called the IV), the plaintext, and optionally additional authenticated data (AAD).…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.