Web Crypto

subtle.decrypt()

Decrypts authenticated ciphertext only if authentication succeeds. Handle failure without exposing plaintext or detailed oracle errors. Tested on Node.js 22.16.0. Example output (one line per entry): ["7", "OperationError"].

Formal Syntax & Structure

nodejs syntaxnodejs
subtle.decrypt(algorithm, key, data)

Parameters & Attributes

NameTypeDescription
algorithmobjectDecryption parameters.
keyCryptoKeyDecryption key.
dataBufferSourceCiphertext bytes.

Return Value

Promise<ArrayBuffer>

Verified plaintext.

Working Example

Example Snippetnodejs
import { webcrypto as crypto } from 'node:crypto';const k = await crypto.subtle.generateKey({ name: 'AES-GCM', length: 128 }, false, ['encrypt', 'decrypt']);const iv = crypto.getRandomValues(new Uint8Array(12));const c = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, k, new Uint8Array([7]));console.log(new Uint8Array(await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, k, c))[0]);const bad = new Uint8Array(c);bad[0] ^= 1;try {    await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, k, bad);}catch (e) {    console.log(e.name);}
Node.js Reference