diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/subtlecrypto/decrypt/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/subtlecrypto/decrypt/index.html')
-rw-r--r-- | files/zh-cn/web/api/subtlecrypto/decrypt/index.html | 94 |
1 files changed, 94 insertions, 0 deletions
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 +--- +<p>{{APIRef("Web Crypto API")}}</p> + +<p><code><strong>SubtleCrypto.decrypt()</strong></code> 以加密数据、算法和密钥为参数返回一个包含明文的 {{jsxref("Promise")}} 对象。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>result</em> = crypto<code>.subtle.decrypt(<em>algorithm</em>, <em>key</em>, <em>data</em>)</code>; +</pre> + +<h3 id="属性">属性</h3> + +<ul> + <li><em><code>algorithm</code></em> 是一个对象,用于指定解密函数及其参数。当没有参数时,<em>algorithm</em> 也可以是包含了算法名称的 {{domxref("DOMString")}} 对象。支持的值<a href="https://www.w3.org/TR/WebCryptoAPI/#algorithm-overview">¹</a> 如下: + + <ul> + <li><code>{"name": "<a href="https://www.w3.org/TR/WebCryptoAPI/#dfn-AesCbcParams">AES-CBC</a>", iv}</code> <code><em>iv</em></code> 与 {{domxref("SubtleCrypto.encrypt()")}} 中描述的相同。</li> + <li><code>{"name": "<a href="https://www.w3.org/TR/WebCryptoAPI/#dfn-AesCtrParams">AES-CTR</a>", counter, length}</code> <code><em>counter</em></code> 和<code><em>length</em></code> 与 {{domxref("SubtleCrypto.encrypt()")}} 中描述的相同</li> + <li><code>{"name": "<a href="https://www.w3.org/TR/WebCryptoAPI/#dfn-AesGcmParams">AES-GCM</a>", iv[, additionalData, tagLength]}</code> <code><em>iv</em></code>, <code><em>additionalData</em></code>和 <code><em>tagLength</em></code> 与 {{domxref("SubtleCrypto.encrypt()")}} 中描述的相同。</li> + <li><code>{"name": "<a href="https://www.w3.org/TR/WebCryptoAPI/#dfn-RsaOaepParams">RSA-OAEP</a>"[, label]}</code> <code><em>label</em></code> 与 {{domxref("SubtleCrypto.encrypt()")}} 中描述的相同。</li> + </ul> + </li> + <li><code><em>key</em></code> 是一个包含了密钥的 {{domxref("CryptoKey")}} 对象,用于解密。</li> + <li><em><code>data</code></em> 是一个包含了待解密的密文的 {{domxref("BufferSource")}} 对象。</li> +</ul> + +<h3 id="返回值">返回值</h3> + +<ul> + <li><code><em>result</em></code> 是一个 {{jsxref("Promise")}} 对象,它会返回由{{glossary("ciphertext")}} 解密的得来的 {{glossary("plaintext")}}。</li> +</ul> + +<h3 id="异常">异常</h3> + +<p>Promise 将会在以下的异常被触发时返回 rejected:</p> + +<dl> + <dt>InvalidAccessError</dt> + <dd>当提供的密钥无法执行请求的操作时(如:解密算法无效,或对指定的解密算法提供了无效的密钥)。</dd> + <dt>OperationError</dt> + <dd>因特定的操作原因导致操作失败时(如:算法的参数大小无效,或解密结果失败)。</dd> +</dl> + +<h2 id="实例">实例</h2> + +<pre class="brush: js">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; +}</pre> + +<p><code><em>iv</em></code> 的含义在 {{domxref("SubtleCrypto.encrypt()")}} 中可以找到。<code><em>ctBuffer</em></code> 是 {{domxref("SubtleCrypto.encrypt()")}} 返回的密文。</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{ SpecName('Web Crypto API', '#SubtleCrypto-method-decrypt', 'SubtleCrypto.decrypt()') }}</td> + <td>{{ Spec2('Web Crypto API') }}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + + + +<p>{{Compat("api.SubtleCrypto.decrypt")}}</p> + +<h2 id="另请参见">另请参见</h2> + +<ul> + <li>{{domxref("Crypto")}} 与 {{domxref("Crypto.subtle")}}。</li> + <li>{{domxref("SubtleCrypto")}} 包含了该接口。</li> +</ul> |