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.
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.
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.
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)
);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.
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.
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.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
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.
The JavaScript Event Loop in Depth: Microtasks, Macrotasks, and Browser Frame Budgeting
An exhaustive dissection of V8 execution contexts, microtask queue drainage, task scheduling, requestAnimationFrame, and preventing main-thread UI jank.
Migrating from Legacy Date to the JavaScript Temporal API: Immutability and Time Zone Precision
A comprehensive migration handbook for replacing the 30-year-old Date object with immutable, IANA-aware Temporal primitives.