安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

decrypt() 方法在 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").

句法

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

参数

返回值

异常

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

decrypt() method supports the same algorithms as the encrypt() 方法。

范例

注意 : 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
  );
}
					

规范

规范 状态 注释
Web Cryptography API
The definition of 'SubtleCrypto.decrypt()' in that specification.
推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
decrypt Chrome 37 Edge 部分支持 12
部分支持 12
Not supported: AES-CTR.
Firefox 34
34
不支持 32 — 34 Disabled
Disabled ). To change preferences in Firefox, visit
IE 部分支持 11
部分支持 11
返回 CryptoOperation 而不是 Promise
Opera 24 Safari 7 WebView Android 37 Chrome Android 37 Firefox Android 34
34
不支持 32 — 34 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 24 Safari iOS 7 Samsung Internet Android 6.0

图例

完整支持

完整支持

部分支持

部分支持

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. Web 加密 API
  2. SubtleCrypto
  3. 方法
    1. decrypt()
    2. deriveBits()
    3. deriveKey()
    4. digest()
    5. encrypt()
    6. exportKey()
    7. generateKey()
    8. importKey()
    9. sign()
    10. unwrapKey()
    11. verify()
    12. wrapKey()
  4. Related pages for Web Crypto API
    1. Crypto
    2. CryptoKey
    3. CryptoKeyPair
    4. RandomSource
    5. Window.crypto

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1