Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
The unwrapKey() method of the SubtleCrypto interface "unwraps" a key. This means that it takes as its input a key that has been exported and then encrypted (also called "wrapped"). It decrypts the key and then imports it, returning a CryptoKey object that can be used in the Web Crypto API.
As with SubtleCrypto.importKey(), you specify the key's import format and other attributes of the key to import details such as whether it is extractable, and which operations it can be used for.
But because unwrapKey() also decrypts the key to be imported, you also need to pass in the key that must be used to decrypt it. This is sometimes called the "unwrapping key".
The inverse of unwrapKey() is SubtleCrypto.wrapKey(): while unwrapKey is composed of decrypt + import, wrapKey is composed of encrypt + export.
A string describing the data format of the key to unwrap. It can be one of the following:
An ArrayBuffer containing the wrapped key in the given format.
unwrappingKeyThe CryptoKey to use to decrypt the wrapped key. The key must have the unwrapKey usage set.
unwrapAlgoAn object specifying the algorithm to be used to decrypt the wrapped key, and any extra parameters as required:
An object defining the type of key to unwrap and providing extra algorithm-specific parameters as required.
A boolean indicating whether it will be possible to export the key using SubtleCrypto.exportKey() or SubtleCrypto.wrapKey().
keyUsagesAn Array indicating what can be done with the key. Possible values of the array are:
A Promise that fulfills with the unwrapped key as a CryptoKey object.
The promise is rejected when one of the following exceptions is encountered:
InvalidAccessError DOMExceptionRaised when the unwrapping key is not a key for the requested unwrap algorithm or if the keyUsages value of that key doesn't contain unwrap.
NotSupported DOMExceptionRaised when trying to use an algorithm that is either unknown or isn't suitable for encryption or wrapping.
SyntaxError DOMExceptionRaised when keyUsages is empty but the unwrapped key is of type secret or private.
TypeErrorRaised when trying to use an invalid format.
The unwrapKey() method supports the same algorithms as the wrapKey() method.
Note: You can try the working examples on GitHub.
In this example we unwrap an AES-GCM symmetric key. The key was exported in "raw" format and encrypted using the AES-KW algorithm, with a key derived from a password.
To unwrap, we ask the user for the password and use PBKDF2 and some salt to derive the AES-KW unwrapping key. The salt needs to be the same as the salt that was used to derive the original AES-KW key wrapping key.
Once we have the unwrapping key we pass it into unwrapKey() with the wrapped key and other parameters. See the complete code on GitHub.
In this example we unwrap an RSA-PSS private signing key. The key was exported in "pkcs8" format and encrypted using the AES-GCM algorithm, with a key derived from a password.
To unwrap, we ask the user for the password and use PBKDF2 and some salt to derive the AES-GCM unwrapping key. The salt needs to be the same as the salt that was used to derive the original AES-GCM key wrapping key.
Once we have the unwrapping key we pass it into unwrapKey() with the wrapped key and other parameters. Note that when using AES-GCM we have to pass the iv value into unwrapKey(), and this must be the same as the iv that was used in the corresponding wrapKey() operation. See the complete code on GitHub.
| Web Cryptography Level 2 # SubtleCrypto-method-unwrapKey |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Sep 25, 2024 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.