[JavaScript]PUBLISHED 2026-11-25//4 min read//VERIFIED 2026-11-25

Web Crypto API: Encrypt Browser Data with AES-GCM and Manage Keys Carefully

Use authenticated encryption, unique nonces, and an explicit key lifecycle instead of inventing a cryptographic format.

EXECUTIVE ARCHITECTURAL OVERVIEW

The Web Crypto API exposes cryptographic primitives through crypto.subtle. AES-GCM combines encryption with authentication, so an application can detect tampering as well as recover plaintext when it holds the correct key.

THE PRODUCTION PROBLEM STATEMENT

Client-side encryption is often presented as a single encrypt() call. Real security depends on where the key comes from, how it is stored and recovered, whether nonces are reused, and what happens when same-origin JavaScript is compromised.

SECTION 01 //

Generate a non-extractable AES-GCM key

Generate the key for the operations the application needs and set extractable to false when exporting the raw key is not part of the design. Non-extractable does not prevent malicious same-origin code from asking the page to use the key while the session is active.

Use a fresh 96-bit random IV for each encryption under a given AES-GCM key. Store the IV alongside the ciphertext; it is not secret, but it must not be reused with that key.

encrypt-local-value.js
const key = await crypto.subtle.generateKey(
  { name: 'AES-GCM', length: 256 },
  false,
  ['encrypt', 'decrypt']
);

const iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt(
  { name: 'AES-GCM', iv },
  key,
  new TextEncoder().encode(secretText)
);
AES-GCM authenticates the encrypted data; persist the key through a deliberate key-management design.
SECTION 02 //

Design key storage and recovery before encrypting data

A key stored next to ciphertext in the same origin does not protect against an attacker who can execute script in that origin. Stronger at-rest protection may require a key derived from a user secret or provisioned through a separate trusted system, each with recovery and usability trade-offs.

Persist a CryptoKey through a storage mechanism that supports structured cloning only after deciding how users recover data and how key rotation works. Never hard-code a reusable encryption secret in shipped JavaScript.

SECTION 03 //

Use encryption for confidentiality, not password storage

AES-GCM is for reversible encryption when the application must later recover the content. Password verification should use a server-side password hashing scheme designed for that purpose, not a fast browser digest.

Web Crypto operations generally require a secure context. Handle authentication failures and key loss as expected states, and avoid writing a custom protocol when a well-reviewed library or platform feature already fits.

TECHNICAL INQUIRY MATRIX

Frequently Asked Questions & Technical Inquiries

Direct, practical answers to common architectural and debugging questions.

No. It prevents direct export of key material through the API, but same-origin malicious script may still ask the application to decrypt data or encrypt attacker-chosen values.

Official References & Specifications:
Faisal Askari

Faisal Askari

Verified Author

Software Engineer & Full-Stack Developer

Personal Website

Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.

SPEC // VERIFIED ENTITY•Karachi, Pakistan (Global Remote)
contact@faisalaskari.top
05/RELATED ENGINEERING GUIDES
CONTINUE STUDY