Firefox Tomorrow

web api instance method

SubtleCrypto: decrypt() method

View on MDN ↗

Secure contextAvailable in workers

The decrypt() method of the SubtleCrypto interface decrypts some encrypted data. It takes as arguments a key to decrypt with, some optional extra parameters, and the data to decrypt (also known as “ciphertext”). It returns a Promise which will be fulfilled with the decrypted data (also known as “plaintext”).

Syntax

decrypt(algorithm, key, data)

Parameters

Return value

A Promise that fulfills with an ArrayBuffer containing the plaintext.

Exceptions

The promise is rejected when the following exceptions are encountered:

  • InvalidAccessError DOMException
    • : Raised when the requested operation is not valid for the provided key (e.g., invalid encryption algorithm, or invalid key for the specified encryption algorithm).
  • OperationError DOMException
    • : Raised when the operation failed for an operation-specific reason (e.g., algorithm parameters of invalid sizes, or there was an error decrypting the ciphertext).

Supported algorithms

The decrypt() method supports the same algorithms as the encrypt() method.

Examples

[!NOTE] You can try the working examples on GitHub.

RSA-OAEP

This code decrypts ciphertext using RSA-OAEP. See the complete code on GitHub.

function decryptMessage(privateKey, ciphertext) {
  return window.crypto.subtle.decrypt(
    { name: "RSA-OAEP" },
    privateKey,
    ciphertext,
  );
}

AES-CTR

This code decrypts ciphertext using AES in CTR mode. Note that counter must match the value that was used for encryption. See the complete code on GitHub.

function decryptMessage(key, ciphertext) {
  return window.crypto.subtle.decrypt(
    { name: "AES-CTR", counter, length: 64 },
    key,
    ciphertext,
  );
}

AES-CBC

This code decrypts ciphertext using AES in CBC mode. Note that iv must match the value that was used for encryption. See the complete code on GitHub.

function decryptMessage(key, ciphertext) {
  // The iv value is the same as that used for encryption
  return window.crypto.subtle.decrypt({ name: "AES-CBC", iv }, key, ciphertext);
}

AES-GCM

This code decrypts ciphertext using AES in GCM mode. Note that iv must match the value that was used for encryption. See the complete code on GitHub.

function decryptMessage(key, ciphertext) {
  // The iv value is the same as that used for encryption
  return window.crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, ciphertext);
}

Specifications

SpecificationsStandards references are available on the canonical MDN page.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.

See also