crypto.subtle.decrypt
Decrypts encrypted data using the specified algorithm and cryptographic key
Syntax
crypto.subtle.decrypt(algorithm, key, data)Parameters
| Parameter | Type | Description |
|---|---|---|
| algorithm | AlgorithmIdentifier | The decryption algorithm and parameters |
| key | CryptoKey | The key to use for decryption |
| data | ArrayBuffer | TypedArray | DataView | The data to decrypt |
Return Value
A Promise that resolves to an ArrayBuffer containing the plaintext
Examples
const decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv },
key,
encryptedData
)
const text = new TextDecoder().decode(decrypted)
console.log(text) // 'Secret message'async function decryptText(cipher: ArrayBuffer, iv: Uint8Array, key: CryptoKey) {
const decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv },
key,
cipher
)
return new TextDecoder().decode(decrypted)
}async function decryptMessage(encrypted: ArrayBuffer, key: CryptoKey, iv: Uint8Array) {
try {
const plain = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, encrypted)
return new TextDecoder().decode(plain)
} catch {
throw new Error('Decryption failed — wrong key or corrupted data')
}
}Understanding crypto.subtle.decrypt
The crypto.subtle.decrypt method in JavaScript decrypts encrypted data using the specified algorithm and cryptographic key. It belongs to the SubtleCrypto object and is one of the most widely used methods for working with subtlecrypto values in modern JavaScript and TypeScript applications.
The method signature is crypto.subtle.decrypt(algorithm, key, data). It accepts 3 parameters: algorithm, key, data. When called, it returns a promise that resolves to an arraybuffer containing the plaintext. Understanding when and how to use decrypt() helps you write more expressive, readable code.
Common use cases for crypto.subtle.decrypt include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like crypto-subtle-encrypt, crypto-subtle-digest, crypto-getrandomvalues, enabling you to chain operations together for complex data manipulation pipelines.
Supported in all modern browsers. Requires a secure context (HTTPS).
Browser Compatibility
Supported in all modern browsers. Requires a secure context (HTTPS).
Related Methods
More SubtleCrypto Methods
Other methods in the SubtleCrypto object
Related Tools
More SubtleCrypto Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.