SubtleCrypto.decrypt() - Web APIs 编辑

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

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

const result = crypto.subtle.decrypt(algorithm, key, data);

Parameters

Return value

Exceptions

The promise is rejected when the following exceptions are encountered:

InvalidAccessError
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
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) {
  return window.crypto.subtle.decrypt(
    {
      name: "AES-CBC",
      iv: 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) {
  return window.crypto.subtle.decrypt(
    {
      name: "AES-GCM",
      iv: iv
    },
    key,
    ciphertext
  );
}

Specifications

SpecificationStatusComment
Web Cryptography API
The definition of 'SubtleCrypto.decrypt()' in that specification.
RecommendationInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:117 次

字数:8255

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文