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.
* Some parts of this feature may have varying levels of support.
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 importKey() method of the SubtleCrypto interface imports a key: that is, it takes as input a key in an external, portable format and gives you a CryptoKey object that you can use in the Web Crypto API.
The function accepts several import formats: see Supported formats for details.
A string describing the data format of the key to import. It can be one of the following:
An ArrayBuffer, a TypedArray, a DataView, or a JSONWebKey object containing the key in the given format.
algorithmAn object defining the type of key to import and providing extra algorithm-specific parameters.
A boolean value 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 array values are:
A Promise that fulfills with the imported key as a CryptoKey object.
The promise is rejected when one of the following exceptions is encountered:
SyntaxError DOMExceptionRaised when keyUsages is empty but the unwrapped key is of type secret or private.
TypeErrorRaised when trying to use an invalid format or if the keyData is not suited for that format.
This API supports four different key import/export formats: Raw, PKCS #8, SubjectPublicKeyInfo, and JSON Web Key.
You can use this format to import or export AES or HMAC secret keys, or Elliptic Curve public keys (ECDSA or ECDH).
In this format the key is supplied as an ArrayBuffer containing the raw bytes for the key.
Note that when importing Elliptic Curve public keys, the data may contain compressed elliptic curve points.
You can use this format to import or export RSA or Elliptic Curve private keys.
The PKCS #8 format is defined in RFC 5208, using the ASN.1 notation:
PrivateKeyInfo ::= SEQUENCE { version Version, privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, privateKey PrivateKey, attributes [0] IMPLICIT Attributes OPTIONAL }The importKey() method expects to receive this object as an ArrayBuffer containing the DER-encoded form of the PrivateKeyInfo. DER is a set of rules for encoding ASN.1 structures into a binary form.
You are most likely to encounter this object in PEM format. PEM format is a way to encode binary data in ASCII. It consists of a header and a footer, and in between, the base64-encoded binary data. A PEM-encoded PrivateKeyInfo looks like this:
-----BEGIN PRIVATE KEY----- MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDAU9BD0jxDfF5OV380z 9VIEUN2W5kJDZ3hbuaDenCxLiAMsoquKTfFaou71eLdN0TShZANiAARMUhCee/cp xmjGc1roj0D0k6VlUqtA+JVCWigXcIAukOeTHCngZDKCrD4PkXDBvbciJdZKvO+l ml2FIkoovZh/8yeTKmjUMb804g6OmjUc9vVojCRV0YdaSmYkkJMJbLg= -----END PRIVATE KEY-----To get this into a format you can give to importKey() you need to do two things:
See the Examples section for more concrete guidance.
You can use this format to import or export RSA or Elliptic Curve public keys.
SubjectPublicKey is defined in RFC 5280, Section 4.1 using the ASN.1 notation:
SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING }Just like PKCS #8, the importKey() method expects to receive this object as an ArrayBuffer containing the DER-encoded form of the SubjectPublicKeyInfo.
Again, you are most likely to encounter this object in PEM format. A PEM-encoded SubjectPublicKeyInfo looks like this:
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3j+HgSHUnc7F6XzvEbD0 r3M5JNy+/kabiJVu8IU1ERAl3Osi38VgiMzjDBDOrFxVzNNzl+SXAHwXIV5BHiXL CQ6qhwYsDgH6OqgKIwiALra/wNH4UHxj1Or/iyAkjHRR/kGhUtjyVCjzvaQaDpJW 2G+syd1ui0B6kJov2CRUWiPwpff8hBfVWv8q9Yc2yD5hCnykVL0iAiyn+SDAk/rv 8dC5eIlzCI4efUCbyG4c9O88Qz7bS14DxSfaPTy8P/TWoihVVjLaDF743LgM/JLq CDPUBUA3HLsZUhKm3BbSkd7Q9Ngkjv3+yByo4/fL+fkYRa8j9Ypa2N0Iw53LFb3B gQIDAQAB -----END PUBLIC KEY-----Just as with PKCS #8, to get this into a format you can give to importKey() you need to do two things:
See the Examples section for more concrete guidance.
You can use JSON Web Key format to import or export RSA or Elliptic Curve public or private keys, as well as AES and HMAC secret keys.
JSON Web Key format is defined in RFC 7517. It describes a way to represent public, private, and secret keys as JSON objects.
A JSON Web Key looks something like this (this is an EC private key):
Note: You can try the working examples on GitHub.
This example imports an AES key from an ArrayBuffer containing the raw bytes to use. See the complete code on GitHub.
This example imports an RSA private signing key from a PEM-encoded PKCS #8 object. See the complete code on GitHub.
This example imports an RSA public encryption key from a PEM-encoded SubjectPublicKeyInfo object. See the complete code on GitHub.
This code imports an ECDSA private signing key, given a JSON Web Key object that represents it. See the complete code on GitHub.
| Web Cryptography Level 2 # SubtleCrypto-method-importKey |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Nov 23, 2025 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.