Symmetric encryption and AES

Introduction

Your phone encrypts everything on it, your Wi-Fi encrypts every packet, and every https:// page you open is encrypted. Almost all of that work is done by symmetric encryption: one secret key that both locks and unlocks, like a hostel locker where the same key opens and closes the door.

The cipher behind most of it is AES, the Advanced Encryption Standard. This lesson explains what AES does in plain words, why the way you use it (the mode) matters as much as the cipher itself, and the one problem symmetric encryption cannot solve on its own.

How it works

A symmetric cipher takes plain text and a key, and gives cipher text that looks random. With the same key, the receiver reverses it. The program below shows the idea in its simplest form, using XOR, an operation that undoes itself: x ^ key ^ key is x again.

// Symmetric encryption in miniature: XOR every character code with a key byte.
// XOR-ing twice with the same key gives the original back, so ONE key both locks and unlocks.
function xorBytes(text, key) {
  const out = [];
  for (let k = 0; k < text.length; k++) out.push(text.charCodeAt(k) ^ key);
  return out;
}

const key = 0x5a; // 90: the shared secret
const codes = xorBytes("Hi Rahim", key);
console.log("encrypted bytes: " + codes.join(" "));

let back = "";
for (const c of codes) back += String.fromCharCode(c ^ key);
console.log("decrypted: " + back);
Open in compiler

A single XOR byte is as weak as Caesar (only 256 keys). Real ciphers make the key space so large that trying every key is impossible. AES, standardised in 2001, works on blocks of 128 bits (16 bytes) with a key of 128, 192 or 256 bits. Even the smallest has 2¹²⁸ possible keys, a number with 39 digits.

Inside, AES repeats a round 10, 12 or 14 times (for 128, 192 and 256-bit keys). Each round:

  1. SubBytes replaces every byte using a fixed table, so the output is not a simple sum of the input.
  2. ShiftRows moves bytes to different positions within the block.
  3. MixColumns mixes each column of four bytes, so one changed input byte soon changes many output bytes.
  4. AddRoundKey XORs in a key derived from the main key for that round.

(The last round leaves out MixColumns.) After a few rounds, every output bit depends on every input bit and every key bit. That spreading is what makes AES hard to attack: there are no patterns to count, unlike the ciphers of the last lesson.

Messages are longer than 16 bytes, so a mode of operation decides how blocks are chained:

ModeHow it worksVerdict
ECBEncrypts each block on its ownNever use it: equal blocks give equal cipher blocks
CBCXORs each block with the previous cipher block first; needs a random IVHides patterns, but does not detect tampering
GCMA counter-based mode that also adds an authentication tagThe usual choice: secret and tamper-evident

Example

The program uses a one-byte toy cipher in place of AES, so you can see every number. The message repeats AAAA, so watch what each mode does with the repeats.

// Why the mode matters. A toy "block cipher" turns one byte into another with a key.
// ECB encrypts every block on its own; CBC mixes in the previous cipher block first.
const key = 77;
const encryptBlock = (b) => ((b ^ key) * 5 + 3) % 256; // a stand-in for AES on a one-byte block

const message = "AAAABBBBAAAA";
const ecb = [];
for (const ch of message) ecb.push(encryptBlock(ch.charCodeAt(0)));
console.log("ECB: " + ecb.join(" "));

const cbc = [];
let previous = 19; // the IV: a random starting block, sent openly with the message
for (const ch of message) {
  previous = encryptBlock(ch.charCodeAt(0) ^ previous);
  cbc.push(previous);
}
console.log("CBC: " + cbc.join(" "));
Open in compiler

Line by line

  1. encryptBlock is a stand-in for AES on one tiny block: XOR with the key, then multiply by 5 and add 3, modulo 256. It can be reversed, like a real cipher, but it is not secure.
  2. ECB encrypts each character on its own. A always becomes 63 and B always becomes 78, so the cipher text repeats wherever the message does.
  3. CBC starts from the IV 19, a random block that is sent openly. Before encrypting a block, it XORs the block with the previous cipher block.
  4. Because each cipher block feeds into the next, the same letter gives a different result each time, and the repeats disappear.

Expected result

ECB: 63 63 63 63 78 78 78 78 63 63 63 63
CBC: 158 221 24 103 11 23 123 71 122 81 212 59

ECB's leak is easy to see in pictures: encrypt an image's pixels in ECB mode and the outline of the picture is still visible, because equal pixel blocks stay equal.

In the real world

  • HTTPS, Wi-Fi (WPA2 and WPA3), phone and laptop disk encryption and messaging apps all use AES or a similar symmetric cipher for the bulk of their data.
  • Many processors have special instructions for AES, so encrypting is fast enough to do for everything.
  • The older standard, DES, had a 56-bit key. In 1998 a purpose-built machine found DES keys by brute force, which is why AES replaced it.
  • Browsers offer AES through the Web Crypto API (crypto.subtle), usually in GCM mode, so web apps never need to write their own cipher.

Common mistakes

  • Using ECB because it is the simplest mode. It leaks patterns.
  • Reusing an IV or nonce with the same key. In GCM this can expose the key stream and let an attacker forge messages.
  • Encrypting without checking integrity. Without an authentication tag, an attacker can change cipher text and the receiver will decrypt garbage, or something worse, without noticing.
  • Writing the key into the source code. Anyone who sees the code, or the app file, has the key.

Best practices

  • Use a standard library (Web Crypto, libsodium, your platform's crypto API), never your own cipher.
  • Prefer authenticated encryption such as AES-GCM, which keeps data secret and detects changes.
  • Use a fresh random IV or nonce for every message.
  • Keep keys out of code and logs: store them in a key store and rotate them.

Summary

Symmetric encryption uses one shared key to encrypt and decrypt, and it carries almost all encrypted data today. AES encrypts 128-bit blocks with 128, 192 or 256-bit keys through 10 to 14 rounds of substitution, shifting, mixing and key mixing, which leaves no patterns to count. The mode matters as much as the cipher: ECB leaks repeats, CBC hides them, and GCM also detects tampering. One problem remains: both sides need the same secret key before they can talk, which is what the next two lessons solve.

Quick check

1. What makes encryption "symmetric"?
2. A file is encrypted with AES in ECB mode, and the same 16-byte block appears five times in it. What does an attacker see?
3. How many rounds does AES use with a 256-bit key?
4. Why is AES-GCM usually preferred to AES-CBC?

Key takeaways

  • Symmetric encryption is fast and does most of the work, but both sides need the same key first.
  • AES's rounds spread every input and key bit over the whole block, leaving no patterns to count.
  • Never use ECB: equal blocks give equal cipher blocks.
  • Use authenticated encryption such as AES-GCM, with a fresh nonce for every message.

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.