Sling Academy
Home/JavaScript/Generate and Manage Keys with the Web Crypto API in JavaScript

Generate and Manage Keys with the Web Crypto API in JavaScript

Last updated: December 13, 2024

The Web Crypto API is a powerful means within JavaScript to perform cryptographic operations, allowing developers to securely generate and manage keys, encrypt, decrypt, sign, and verify data. In this article, we'll explore how to generate and manage cryptographic keys using this API. This exploration includes a series of examples to illuminate both symmetric and asymmetric key operations.

Introduction to Web Crypto API

The Web Crypto API is part of the secure context protocol, rooted deeply within the browser security model, making it a safe choice for cryptographic tasks on the web. Unlike other cryptographic libraries, the Web Crypto API leverages the browser’s built-in security features to manage keys and perform operations without exposing sensitive data.

Generating a Key Pair

The first step in utilizing cryptography in your web applications is understanding how to generate keys. The Web Crypto API supports various cryptographic algorithms, but we'll focus on the RSA-OAEP algorithm, which is commonly used for encryption. Below is an example of how to generate a public and private key pair using this algorithm.

async function generateKeyPair() {
  const keyPair = await window.crypto.subtle.generateKey({
    name: "RSA-OAEP",
    modulusLength: 2048,
    publicExponent: new Uint8Array([1, 0, 1]),
    hash: "SHA-256",
  }, true, ["encrypt", "decrypt"]);

  console.log("Public Key: ", keyPair.publicKey);
  console.log("Private Key: ", keyPair.privateKey);
}

generateKeyPair();

This function generates an RSA key pair, with a 2048-bit modulus length and SHA-256 hash. The keys are specified to be used for encryption and decryption operations.

Exporting and Importing Keys

Once a key pair is generated, you might need to export the keys to share or store them. Exporting keys converts them into a format that can be easily stored or transferred—commonly, the JWK (JSON Web Key) format.

async function exportPublicKey(key) {
  const exported = await window.crypto.subtle.exportKey("jwk", key.publicKey);
  console.log("Exported Public Key: ", exported);
  return exported;
}

Importing keys back into the application is similarly supported:

async function importPublicKey(jwk) {
  const key = await window.crypto.subtle.importKey(
    "jwk",
    jwk,
    {
      name: "RSA-OAEP",
      hash: "SHA-256"
    },
    true,
    ["encrypt"]
  );
  console.log("Imported Public Key: ", key);
  return key;
}

Encrypting and Decrypting Data

With an RSA key pair, you can perform encryption and decryption operations to keep data secure. The example below demonstrates encrypting data with a public key and decrypting with a private key:

async function encryptData(publicKey, data) {
  const encoded = new TextEncoder().encode(data);
  return await window.crypto.subtle.encrypt({
    name: "RSA-OAEP"
  }, publicKey, encoded);
}

async function decryptData(privateKey, ciphertext) {
  const decrypted = await window.crypto.subtle.decrypt({
    name: "RSA-OAEP"
  }, privateKey, ciphertext);
  return new TextDecoder().decode(decrypted);
}

Conclusion

Understanding and using the Web Crypto API can significantly enhance the security of your web applications. It provides the necessary tools to efficiently generate keys and manage cryptographic tasks securely, leveraging browser-based standards. By following these examples, you should have a foundational understanding of key generation, management, and cryptographic operations using JavaScript in a modern web environment.

Continue experimenting with different algorithms and exploring the wealth of security operations available with the Web Crypto API to further harden the data exchanges between your applications and servers.

Next Article: Ensure Secure Communications Using JavaScript Web Crypto

Previous Article: Encrypt and Decrypt Data Using the Web Crypto API in JavaScript

Series: Web APIs – JavaScript Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration