After this lesson, you will be able to: Use symmetric encryption correctly: AES-256-GCM, block vs stream ciphers, modes of operation, why ECB is broken, and IV/nonce management.
Symmetric encryption uses one shared key for both encryption and decryption and does the bulk-data work in every secure system. This lesson covers AES, block vs stream ciphers, modes of operation, the famous ECB failure, and the rules for IVs and nonces that make or break security.
AES (Advanced Encryption Standard) is the symmetric cipher used almost everywhere, typically with a 256-bit key (AES-256). A block cipher encrypts fixed-size blocks (AES uses 128-bit blocks); a stream cipher encrypts a byte at a time against a keystream (ChaCha20 is the modern favorite, especially on mobile). Both are secure when used correctly; the failures come from how you chain blocks (the mode) and how you manage nonces.
A mode defines how a block cipher encrypts data longer than one block. ECB (Electronic Codebook) encrypts each block independently, so identical plaintext blocks produce identical ciphertext blocks, leaking structure (the infamous 'ECB penguin' image is still recognizable after encryption). Never use ECB. CBC chains blocks with an IV but needs separate integrity protection. GCM (Galois/Counter Mode) is the modern default: it encrypts and authenticates in one step (authenticated encryption), so tampering is detected automatically.
Authenticated encryption with a random IV per message. Never reuse an IV with the same key.
const key = await crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"]);// A FRESH random IV for every message. Reusing one with the same key is catastrophic.const iv = crypto.getRandomValues(new Uint8Array(12));const ciphertext = await crypto.subtle.encrypt({ name: "AES-GCM", iv },key,new TextEncoder().encode("secret message"),);// Store iv alongside ciphertext (the IV is not secret, but must be unique).
An IV (initialization vector) or nonce makes encrypting the same plaintext twice produce different ciphertext. The rules: it must be unique per message under a given key, it does not need to be secret (store it with the ciphertext), and for GCM, reusing an IV with the same key is catastrophic (it can leak the authentication key and the plaintext). Use a cryptographically random IV (or a guaranteed-unique counter). Most IV failures come from a fixed or repeated nonce.
Pick the best reason.
Using ECB because it is the simplest default in a library. Reusing an IV/nonce with the same key (especially in GCM). Encrypting without authenticating (use GCM, or encrypt-then-MAC). Hardcoding or reusing keys. Confusing encryption with encoding (Base64 is not encryption). Rolling your own mode. Storing the key next to the ciphertext with no protection.
Sign in and purchase access to unlock this lesson.