From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../zh-cn/web/api/subtlecrypto/decrypt/index.html | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 files/zh-cn/web/api/subtlecrypto/decrypt/index.html (limited to 'files/zh-cn/web/api/subtlecrypto/decrypt/index.html') diff --git a/files/zh-cn/web/api/subtlecrypto/decrypt/index.html b/files/zh-cn/web/api/subtlecrypto/decrypt/index.html new file mode 100644 index 0000000000..2f303bda6c --- /dev/null +++ b/files/zh-cn/web/api/subtlecrypto/decrypt/index.html @@ -0,0 +1,94 @@ +--- +title: SubtleCrypto.decrypt() +slug: Web/API/SubtleCrypto/decrypt +translation_of: Web/API/SubtleCrypto/decrypt +--- +

{{APIRef("Web Crypto API")}}

+ +

SubtleCrypto.decrypt() 以加密数据、算法和密钥为参数返回一个包含明文的 {{jsxref("Promise")}} 对象。

+ +

语法

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

属性

+ + + +

返回值

+ + + +

异常

+ +

Promise 将会在以下的异常被触发时返回 rejected:

+ +
+
InvalidAccessError
+
当提供的密钥无法执行请求的操作时(如:解密算法无效,或对指定的解密算法提供了无效的密钥)。
+
OperationError
+
因特定的操作原因导致操作失败时(如:算法的参数大小无效,或解密结果失败)。
+
+ +

实例

+ +
const decryptText = async (ctBuffer, iv, password) => {
+    const pwUtf8 = new TextEncoder().encode(password);
+    const pwHash = await crypto.subtle.digest('SHA-256', pwUtf8);
+
+    const alg = { name: 'AES-GCM', iv: iv };
+    const key = await crypto.subtle.importKey('raw', pwHash, alg, false, ['decrypt']);
+
+    const ptBuffer = await crypto.subtle.decrypt(alg, key, ctBuffer);
+
+    const plaintext = new TextDecoder().decode(ptBuffer);
+
+    return plaintext;
+}
+ +

iv 的含义在 {{domxref("SubtleCrypto.encrypt()")}} 中可以找到。ctBuffer 是 {{domxref("SubtleCrypto.encrypt()")}} 返回的密文。

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{ SpecName('Web Crypto API', '#SubtleCrypto-method-decrypt', 'SubtleCrypto.decrypt()') }}{{ Spec2('Web Crypto API') }}Initial definition.
+ +

浏览器支持

+ + + +

{{Compat("api.SubtleCrypto.decrypt")}}

+ +

另请参见

+ + -- cgit v1.2.3-54-g00ecf