The SubtleCrypto.decrypt()
method returns a Promise
of the plaintext corresponding to the ciphertext data, algorithm and key given as parameters.
var result = crypto.subtle.decrypt(algorithm, key, data);
algorithm
is an object specifying the encryption function to be used and its parameters; if there are no parameters, algorithm can be a DOMString
with the algorithm name. Supported values¹ are: {"name": "AES-CBC", iv}
where iv
is as supplied to SubtleCrypto.encrypt()
.{"name": "AES-CTR", counter, length}
where counter
and length
are as supplied to SubtleCrypto.encrypt()
.{"name": "AES-GCM", iv[, additionalData, tagLength]}
where iv
, additionalData
, tagLength
are as supplied to SubtleCrypto.encrypt()
.{"name": "RSA-OAEP"[, label]} where label is
as supplied to SubtleCrypto.encrypt()
.key
is a CryptoKey
containing the key to be used for decryption.data
is a BufferSource
containing the data to be decrypted, the ciphertext.result
is a Promise
that returns the plaintext generated by the decryption of the ciphertext.The promise is rejected when the following exceptions are encountered:
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; }
The iv
is as supplied to SubtleCrypto.encrypt()
; the ctBuffer
is the ciphertext returned from SubtleCrypto.encrypt()
.
Specification | Status | Comment |
---|---|---|
Web Cryptography API The definition of 'SubtleCrypto.decrypt()' in that specification. | Recommendation | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 37 | 12 | 34
|
11
|
24 | 7 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 37 | 37 | 12 | 34
|
24 | 7 | 6.0 |
Crypto
and Crypto.subtle
.SubtleCrypto
, the interface it belongs to.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt