Chapter 02

Building blocks

Every cryptosystem, from a Roman cipher to TLS 1.3, is built from the same few parts. Learn these and the rest of the primer will make sense.

The core components

TermWhat it meansExample
Plaintext (cleartext)The original readable data. "Cleartext" sometimes means data that was never meant to be encrypted, but most people use the two words interchangeably.Transfer £500
Algorithm (cipher)The public set of steps (maths) that turns plaintext into ciphertext and back again.AES, Caesar, RSA
KeyThe secret value that picks which of the algorithm's many possible transformations is used. Same algorithm with a different key gives a completely different result.Caesar shift of 3; a 256-bit AES key
CiphertextThe scrambled output. It should look like random noise to anyone without the key.9f3ac1d077be…
CryptosystemThe whole thing: algorithm, keys, protocols and the software and hardware that run them.BitLocker, TLS
Key spaceThe total number of possible keys. For an n-bit key that's 2n.AES-128 has 2128 ≈ 3.4×1038 keys

Cryptovariables

Cryptovariable is the older, mostly military and government word for a key: the variable you feed into a fixed algorithm. You'll still see it in CISSP material. More broadly, it covers every adjustable input that changes a cipher's output for the same plaintext:

🔑 Key

The secret. Its length (in bits) sets the key space and so the brute-force resistance.

🎲 IV (initialisation vector)

A random or unpredictable starting value, so the same plaintext under the same key encrypts differently each time. It isn't secret, but it must not be predictable (CBC) or reused (GCM).

🔢 Nonce

A "number used once". It doesn't have to be random, but it must never repeat with the same key. Counters work well. Reusing a nonce in GCM or a stream cipher is catastrophic.

🧂 Salt

A random, non-secret value added before hashing passwords, so identical passwords produce different hashes. This defeats rainbow tables.

🔁 Work factor

The iteration count or cost setting in key-stretching functions (PBKDF2, bcrypt, Argon2) that makes each guess deliberately slow.

📏 Block size and rounds

Algorithm parameters. AES always uses 128-bit blocks, with 10, 12 or 14 rounds for 128, 192 or 256-bit keys.

Key space and brute force

Each extra bit doubles the number of keys. That exponential growth is why moving from 56-bit DES to 128-bit AES took brute force from "a few days" to "longer than the age of the universe".

Brute-force calculator

On average an attacker finds the key after searching half the key space. Quantum computers running Grover's algorithm would in theory halve the effective key length, which is why AES-256 is recommended for long-term security.

XOR: the workhorse of cryptography

Exclusive OR (XOR, written ⊕) compares two bits. The result is 1 if they're different and 0 if they're the same. That's all there is to it, but it shows up in almost every modern cipher because it has one very useful property:

XOR undoes itself(P ⊕ K) ⊕ K = P. XOR with a key to encrypt, then XOR with the same key again to decrypt.
ABA ⊕ B
000
011
101
110

Why XOR rather than AND or OR? With AND, a 0 in the key always outputs 0. With OR, a 1 always outputs 1. Both leak the plaintext. XOR outputs 0 and 1 equally often whatever the plaintext bit is. If the key bit is random, the output bit is random too.

Flip the bits

Click any plaintext or key bit to flip it. The ciphertext row is plaintext ⊕ key, and the bottom row XORs the ciphertext with the key again.

XOR a message with a key

Plaintext (hex)

Key stream (hex)

Ciphertext = plaintext ⊕ key (hex)

Ciphertext ⊕ key, back to text

The one-time pad and the "two-time pad"

XOR with a key that is truly random, at least as long as the message, kept secret, and never reused is the one-time pad (Vernam cipher, 1917). Claude Shannon proved in 1949 that it's perfectly secret: the ciphertext gives away nothing except the length. Soviet spies used pads, and so did the Moscow–Washington hotline.

It's rarely used in practice because the key is as big as the data and you have to deliver it securely in advance. Reuse is fatal. If two messages share a key, XORing the ciphertexts cancels the key out: C1 ⊕ C2 = P1 ⊕ P2. The US Venona project broke reused Soviet pads exactly this way, and WEP's IV reuse (Chapter 07) is the same mistake.

Key reuse leaks plaintext

C1 ⊕ C2 (what the eavesdropper can compute)

P1 ⊕ P2 (no key involved)

(C1 ⊕ C2) ⊕ guess reveals message 2:

Confusion, diffusion and the avalanche effect

Claude Shannon named two properties every strong cipher needs:

Classical ciphers use one or the other. A Caesar cipher substitutes and a rail fence transposes, which is why they're weak. Modern ciphers alternate both over many rounds. Done well, this gives the avalanche effect: change one input bit and about half the output bits flip.

Avalanche effect (SHA-256)

Hash A

Hash B

Each square is one of the 256 output bits. Highlighted squares changed.

Block ciphers, stream ciphers and modes

Block ciphers

Encrypt fixed-size chunks (AES: 128 bits / 16 bytes). Data that doesn't fill a block is padded. A mode of operation controls how the blocks are chained together.


AES, DES, 3DES, Blowfish, Twofish

Stream ciphers

Generate a pseudo-random keystream from key and nonce, and XOR it with the data one bit or byte at a time. Fast, with no padding, but a reused keystream is fatal.


RC4 (broken), ChaCha20, A5/1 (GSM)

ModeHow it worksVerdict
ECB (Electronic Codebook)Each block is encrypted separately with the same key.avoid Identical plaintext blocks give identical ciphertext, so patterns show through.
CBC (Cipher Block Chaining)Each block is XORed with the previous ciphertext block before encrypting. Starts with an IV.legacy Vulnerable to padding-oracle attacks if it's not MAC'd correctly. Removed from TLS 1.3.
CTR (Counter)Encrypts nonce+counter to make a keystream, which turns a block cipher into a stream cipher.good Parallelisable, but it has no integrity of its own.
GCM (Galois/Counter)CTR encryption plus a built-in authentication tag.preferred This is AEAD: confidentiality and integrity in one. The default in TLS 1.3 and WPA3.
Why ECB leaks: the "penguin" problem

Each 2×2 pixel block is "encrypted" with a toy keyed function and the output is drawn as a colour. In ECB, identical input blocks always give identical output colours. CBC mixes in the previous block, so the pattern disappears.

Plaintext image
ECB mode
CBC mode

Key management

Strong algorithms rarely fail. Key handling often does. A good key lifecycle covers:

  1. Generation from a cryptographically secure random number generator (CSPRNG). Weak randomness has broken real systems, such as the 2008 Debian OpenSSL bug.
  2. Distribution through key exchange (Diffie-Hellman), key wrapping, or out-of-band delivery.
  3. Storage in an HSM, a TPM, a secure enclave or a cloud KMS. Never hard-code keys in source code.
  4. Use for one purpose per key, under least privilege. Split knowledge and dual control protect the most sensitive keys.
  5. Rotation on a schedule (cryptoperiod) and immediately after compromise.
  6. Revocation, archival and destruction, including key escrow where the law or the business requires it.
Passwords aren't keysHuman passwords have far too little entropy to use directly as keys. Key derivation functions (PBKDF2, bcrypt, scrypt, Argon2) combine the password with a salt and a deliberately high work factor to stretch it into a key. WPA2-Personal uses PBKDF2 with 4,096 iterations to turn your Wi-Fi passphrase into the 256-bit PMK.

Knowledge check