Skip to content

Lesson 6 of 9 · 27m

Web Crypto in Practice: Keys, Encryption, and What Not to Build

SubtleCrypto is a low-level primitive toolkit — key generation, AES-GCM, ECDSA, non-extractable keys, and the boundaries of browser cryptography.

Web Crypto is one of the most consequential browser APIs and one of the easiest to misuse. It gives you strong primitives, not a protocol. AES-GCM does not know what your key means, ECDSA does not solve identity, and encrypting data in the browser does not protect it from malicious JavaScript running in that same browser. Know that boundary before design review.

The API is intentionally subtle

const key = await crypto.subtle.generateKey(
  { name: "AES-GCM", length: 256 },
  true,
  ["encrypt", "decrypt"]
);

const iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt(
  { name: "AES-GCM", iv },
  key,
  new TextEncoder().encode("field report")
);

AES-GCM is authenticated encryption: ciphertext includes integrity protection, and decryption fails if bytes are altered. It is usually the correct symmetric primitive for application data. AES-CBC without a separate MAC is how older systems invite padding-oracle history back into the room.

Nonces are operational state

For AES-GCM, reuse of the same key/nonce pair is catastrophic for confidentiality and integrity. Random 96-bit nonces are common, but high-volume systems need counting, key rotation, or XChaCha20-style constructions chosen by a cryptographer. Store the nonce next to the ciphertext; it is not secret. Losing it makes decryption impossible.

Keys can stay non-extractable

const pair = await crypto.subtle.generateKey(
  { name: "ECDSA", namedCurve: "P-256" },
  false,
  ["sign", "verify"]
);

extractable: false means JavaScript cannot export raw private key material. That is valuable against casual exfiltration, not a security boundary against code running in the page. For storage, IndexedDB can hold CryptoKey objects directly in modern browsers; do not serialize private keys to JSON unless export is genuinely part of the design.

Browser crypto does not save you from server trust

End-to-end encryption in a web app is constrained by delivery: if the server can ship different JavaScript to one user, it can often ship key-exfiltrating JavaScript. That does not make browser E2EE useless — it raises the bar for passive storage compromise and server operators — but it is not the same trust model as a pinned native client. Be precise in threat models and marketing copy.

Field notes

  • Use crypto.getRandomValues, never Math.random, for security values.
  • PBKDF2 is available, but prefer modern password hashing memory-hard designs server-side; browser key derivation has different tradeoffs.
  • subtle is only available in secure contexts. Localhost counts; plain HTTP intranet deployments do not.
  • If your design includes phrases like "we encrypt passwords" or "we invented a token format," stop and bring in cryptography review.

Was this lesson helpful?

Sign in to rate