1. Caesar Cipher & Substitution
The oldest encryption algorithms
Imagine you want to send a secret message to a friend. The simplest thing you could do is shift every letter forward by a fixed amount: A becomes D, B becomes E, C becomes F, and so on. Anyone who intercepts the message sees gibberish — unless they know the shift. That's the Caesar cipher, named after Julius Caesar, who reportedly used a shift of 3 for his military correspondence.
The Caesar cipher is the simplest form of encryption. It introduces the two fundamental concepts that every cipher relies on: a key (the shift amount) and a reversible transformation (shift forward to encrypt, shift backward to decrypt).
A Simple Example
Plaintext
HELLO WORLD
Key: shift = 3
H → K E → H L → O L → O O → R
W → Z O → R R → U L → O D → G
Ciphertext
KHOOR ZRUOG
Plaintext
HELLO WORLD
Ciphertext
KHOOR ZRUOG
To decrypt, the recipient shifts every letter backward by 3. K becomes H, H becomes E, and the original message reappears.
How the Algorithm Works
-
Choose a key
Pick a number between 1 and 25 — this is your shift amount.
-
For each letter in the message
Convert the letter to a number (A=0, B=1, ... Z=25).
-
Add the key
Compute
(letter + key) mod 26. The modulo wraps Z back around to A. -
Convert back to a letter
The result is the encrypted character. Repeat for every letter.
Pseudocode
function caesar_encrypt(plaintext, key):
output = ""
for ch in plaintext:
if ch is a letter:
base = 'A' if ch is uppercase else 'a'
output += chr((ord(ch) - base + key) mod 26 + base)
else:
output += ch // leave spaces, punctuation unchanged
return output
function caesar_decrypt(ciphertext, key):
return caesar_encrypt(ciphertext, 26 - key)
Notice that decryption is just encryption with the "opposite" key. This symmetry is a property shared by many ciphers.
The Full Substitution Table (shift = 3)
Plain: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Cipher: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
Every letter maps to exactly one other letter. This mapping is the substitution table.
Breaking the Caesar Cipher
Critical Weakness
The Caesar cipher has only 25 possible keys. An attacker can simply try all 25 shifts and read whichever one produces readable text. This is called a brute-force attack.
Brute-forcing KHOOR
Shift 1: JGNNQ
Shift 2: IFMMP
Shift 3: HELLO ← readable!
Shift 4: GDKKN
Shift 5: FCJJM
...
With only 25 possibilities, a human can break it by hand in minutes.
General Substitution Cipher
The Caesar cipher restricts itself to simple shifts, but why not scramble the alphabet in any order? A general substitution cipher maps each letter to any other letter, as long as each mapping is unique.
A random substitution table
Plain: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Cipher: Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
Now A→Q, B→W, C→E, and so on. The key is the entire scrambled alphabet.
This cipher has 26! (about 4 × 10²⁶) possible keys — far too many to brute-force. So is it secure?
Frequency Analysis — Why Substitution Fails
No. In English, the letter E appears about 13% of the time, T about 9%, A about 8%, and so on. A substitution cipher preserves these frequencies — it just relabels them.
Frequency analysis attack
In the ciphertext, the most common letter is 'Z' (appears 13.2%)
In English, the most common letter is 'E' (appears ~13%)
→ Z probably encodes E
The second most common is 'M' (appears 9.1%)
In English, the second most common is 'T' (appears ~9%)
→ M probably encodes T
Continue for all letters...
With enough ciphertext, an attacker can reconstruct the entire substitution table statistically. This technique, called frequency analysis, was first described by the Arab polymath Al-Kindi in the 9th century.
The Core Problem
Substitution ciphers are deterministic: the same plaintext letter always produces the same ciphertext letter. This leaks the statistical structure of the original message. A good cipher must destroy this pattern.
Encryption Terminology
Before we move on, let's establish the vocabulary that every page in this series uses:
| Term | Meaning |
|---|---|
| Plaintext | The original, readable message |
| Ciphertext | The encrypted, scrambled output |
| Key | The secret value used to encrypt and decrypt |
| Encrypt | Transform plaintext into ciphertext using the key |
| Decrypt | Transform ciphertext back into plaintext using the key |
| Cipher | An algorithm for encryption and decryption |
| Symmetric cipher | Same key encrypts and decrypts (like Caesar) |
| Asymmetric cipher | Different keys for encryption and decryption (pages 6-8) |
Key Takeaways
- The Caesar cipher shifts each letter by a fixed amount — the key is the shift
- It has only 25 possible keys and is trivially broken by brute force
- General substitution ciphers have 26! keys but are broken by frequency analysis
- The fundamental flaw: the same letter always encrypts to the same letter, preserving statistical patterns
- A strong cipher must make identical plaintext letters produce different ciphertext in different positions
Try It Yourself — Caesar Cipher
Type a message and adjust the shift to see the Caesar cipher in action. Watch the ciphertext update live as you type or slide.
Ciphertext
Character Mapping
Brute Force — Crack a Caesar Cipher
Paste a ciphertext below to see all 26 possible shifts. The row most likely to be English (by letter-frequency chi-squared analysis) is highlighted.
The Caesar and substitution ciphers fail because they're deterministic — the same input always produces the same output. What if we combined the plaintext with a random key, bit by bit? That's the idea behind XOR encryption and the one-time pad.