The SubtleCrypto.decrypt()
method returns a Promise
of the plaintext corresponding to the ciphertext data, algorithm and key given as parameters.
Syntax
var result = crypto.decrypt(algorithm, key, data)
;
Parameters
algorithm
is an object specifying the encryption function to be used and its parameters; if there are no parameters, algorithm can be aDOMString
with the algorithm name. Supported values¹ are:{"name": "AES-CBC", iv}
whereiv
is as supplied toSubtleCrypto.encrypt()
.{"name": "AES-CTR", counter, length}
wherecounter
andlength
are as supplied toSubtleCrypto.encrypt()
.{"name": "AES-GCM", iv[, additionalData, tagLength]}
whereiv
,additionalData
,tagLength
are as supplied toSubtleCrypto.encrypt()
.{"name": "RSA-OAEP"[, label]} where label is
as supplied toSubtleCrypto.encrypt()
.
key
is aCryptoKey
containing the key to be used for decryption.data
is aBufferSource
containing the data to be decrypted, the ciphertext.
Return value
result
is aPromise
that returns the plaintext generated by the decryption of the ciphertext.
Exceptions
The promise is rejected when the following exceptions are encountered:
- InvalidAccessError
- when the requested operation is not valid for the provided key (e.g. invalid encryption algorithm, or invalid key for specified encryption algorithm).
- OperationError
- when the operation failed for an operation-specific reason (e.g. algorithm parameters of invalid sizes, or the result of the decryption is a fail).
Example
const pwUtf8 = new TextEncoder().encode('my 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);
The iv
is as supplied to SubtleCrypto.encrypt()
; the ctBuffer
is the ciphertext returned from SubtleCrypto.encrypt()
.
Specifications
Specification | Status | Comment |
---|---|---|
Web Cryptography API The definition of 'SubtleCrypto.decrypt()' in that specification. |
Recommendation | Initial definition. |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 37 | (Yes) | 34 (34) | No support | ? | No support |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | 37 | (Yes) | 34.0 (34) | No support | ? | No support |
See also
Crypto
andCrypto.subtle
.SubtleCrypto
, the interface it belongs to.