Web Crypto API AES (CBC/GCM/CTR) Export Keys with JWK format

by Anish

Posted on Monday september 17, 2018

This sample chapter extracted from the book, Cryptography for JavaScript Developers.

The Crypto interface represents an interface to general purpose cryptographic functionality including a cryptographically strong pseudo-random number generator seeded with truly random values.

The SubtleCrypto.generateKey() method returns a Promise of a newly generated CryptoKey

The Syntax

var result = crypto.subtle.generateKey(algo, extractable, keyUsages);
  • algo: Supported algo are: AES-CBC, AES-CTR, AES-GCM, RSA-OAEP, AES-KW, HMAC, RSASSA-PKCS1-v1_5, ECDSA, ECDH, and DH.

  • extractable is a Boolean indicating if the key can be extracted from the CryptoKey object at a later stage

  • keyUsages is an Array indicating what can be done with the newly generated key. Possible values of the array are for AES encryption:

    • encrypt, allowing the key to be used for encrypting messages.
    • decrypt, allowing the key to be used for decrypting messages.
    • wrapKey, allowing the key to wrap a symmetric key for usage (transfer, storage) in unsecure environments.
    • unwrapKey, allowing the key to unwrap a symmetric key for usage (transfer, storage) in unsecure environments.

The SubtleCrypto.exportKey() method returns a Promise of the key encrypted in the requested format

var result = crypto.subtle.exportKey(format, key);

The Demo

KTY: ALGO: use: key:

java script example AES-GCM generateKey and Export Keys with JWK format

 function AESGCM_EXPORT_KEYS() {

    var iv = crypto.getRandomValues(new Uint8Array(16));
            name: "AES-GCM",
            length: 256, //can be  128, 192, or 256
        true, // the key is extractable (i.e. can be used in exportKey)
        ["encrypt", "decrypt", "wrapKey", "unwrapKey"] //can "encrypt", "decrypt" or  "wrapKey", or "unwrapKey"
    ).then(function(aesgcmkey) {
        var secretKey = aesgcmkey;
        return crypto.subtle.exportKey("jwk", secretKey).then(function(result) {
        key = result;
        document.getElementById("kty").value = key.kty;
        document.getElementById("key_ops").value = key.key_ops;
        document.getElementById("key_algo").value = key.alg
        document.getElementById("key").value = btoa(key.k);
    }, failAndLog);
    .catch(function(err) {


    <button type="button" onclick="AESGCM_EXPORT_KEYS()">AES256-GCM-GENERATE KEY AND EXPORT IN JWK</button>
    KTY: <input type="text" id="kty" size="10">
    ALGO: <input type="text" id="key_algo" size="10">
    use: <input type="text" id="key_ops" size="20">
    key: <input type="text" id="key" size="32">

Download the sample code here
Next Reading Perform AES Encryption Using importKey Method

Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from by offering all my Nine book for just $9

python Cryptography Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Kubernetes for DevOps

Hello Dockerfile

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers