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 wrapKey() method of the SubtleCrypto interface "wraps" a key. This means that it exports the key in an external, portable format, then encrypts the exported key. Wrapping a key helps protect it in untrusted environments, such as inside an otherwise unprotected data store or in transmission over an unprotected network.
As with SubtleCrypto.exportKey(), you specify an export format for the key. To export a key, it must have CryptoKey.extractable set to true.
But because wrapKey() also encrypts the key to be exported, you also need to pass in the key that must be used to encrypt it. This is sometimes called the "wrapping key".
The inverse of wrapKey() is SubtleCrypto.unwrapKey(): while wrapKey is composed of export + encrypt, unwrapKey is composed of import + decrypt.
A string describing the data format in which the key will be exported before it is encrypted. It can be one of the following:
rawRaw format.
pkcs8PKCS #8 format.
spkiSubjectPublicKeyInfo format.
jwkJSON Web Key format.
keyThe CryptoKey to wrap.
wrappingkeyThe CryptoKey used to encrypt the exported key. The key must have the wrapKey usage set.
wrapAlgoAn object specifying the algorithm to be used to encrypt the exported key, and any required extra parameters:
A Promise that fulfills with an ArrayBuffer containing the encrypted exported key.
The promise is rejected when one of the following exceptions is encountered:
InvalidAccessError DOMExceptionRaised when the wrapping key is not a key for the requested wrap algorithm.
NotSupported DOMExceptionRaised when trying to use an algorithm that is either unknown or isn't suitable for encryption or wrapping.
TypeErrorRaised when trying to use an invalid format.
All algorithms that are usable for encryption are also usable for key wrapping, as long as the key has the "wrapKey" usage set. For key wrapping you have the additional option of AES-KW.
AES-KW is a way to use the AES cipher for key wrapping.
One advantage of using AES-KW over another AES mode such as AES-GCM is that AES-KW does not require an initialization vector. To use AES-KW, the input must be a multiple of 64 bits.
AES-KW is specified in RFC 3394.
Note: You can try the working examples out on GitHub.
This example wraps an AES key. It uses "raw" as the export format and AES-KW, with a password-derived key, to encrypt it. See the complete code on GitHub.
This example wraps an RSA private signing key. It uses "pkcs8" as the export format and AES-GCM, with a password-derived key, to encrypt it. See the complete code on GitHub.
This example wraps an RSA public encryption key. It uses "spki" as the export format and AES-CBC, with a password-derived key, to encrypt it. See the complete code on GitHub.
This example wraps an ECDSA private signing key. It uses "jwk" as the export format and AES-GCM, with a password-derived key, to encrypt it. See the complete code on GitHub.
| Web Cryptography Level 2 # SubtleCrypto-method-wrapKey |
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.