security/caesar-cipher-why-25-guesses-break-it-and-what-real-encryption-does-in.md
Caesar cipher: why 25 guesses break it, and what real encryption does instead
The Caesar cipher swaps each letter for one a fixed distance along the alphabet. Slide the key yourself, then play the attacker and break it in just 25 tries.
Learn what a key, plaintext and ciphertext are, why a huge key space still isn't enough on its own, and what real encryption like AES does differently.

Imagine you want to pass a note across a classroom and keep it private from whoever is in between. A very old trick is to swap every letter for another one a fixed distance along the alphabet. It is called the Caesar cipher, and it is the simplest way to see what encryption means: scrambling a message so that only someone with the right secret can read it. It is also a perfect way to see why that trick fails, and what real encryption does differently.
The idea: slide the alphabet
Write the alphabet in a row. Write it again underneath, but slid along by a few places. The number of places is the key, the secret the sender and receiver share. The original message is the plaintext; the scrambled version is the ciphertext. The Roman writer Suetonius reports that Julius Caesar used a shift of three, which is where the name comes from.
The same thing in code
A computer does this with arithmetic. Every letter gets a number (A is 0, B is 1, and so on up to Z at 25). Add the key, then wrap around past Z with the remainder operator %, which gives what is left after dividing. Here it is in Python:
def caesar(text, shift):
out = []
for ch in text:
if "A" <= ch <= "Z":
out.append(chr((ord(ch) - 65 + shift) % 26 + 65))
else:
out.append(ch)
return "".join(out)
print(caesar("MEET AT NOON", 3))
Running it prints:
PHHW DW QRRQ
ord turns a character into its code number (65 for A), and chr turns a number back into a character. To decrypt, shift the other way: caesar(secret, -3). Python's % keeps the result between 0 and 25 even for negative numbers, so the wrap-around works in both directions. A shift of 13 is a famous special case called ROT13: because 13 is half of 26, applying it twice gives back the original text.
Now play the attacker
Suppose you are the person in the middle. You can see the ciphertext, but you do not know the key. How many keys could there be? Shifting by 0 changes nothing and shifting by 26 wraps back to 0, so the useful keys are 1 to 25. That is only 25 guesses. This is a brute-force attack: try every possible key until one gives readable text.
A person finds the readable line in seconds, and a computer in far less. Even without a computer you could do it with a pencil. The key space, the total number of possible keys, is just too small.
Making the key space bigger is not enough
What if we let each letter map to any other letter, not a fixed slide? That is a substitution cipher, and it has 26 × 25 × 24 × ... × 1 = 403,291,461,126,605,635,584,000,000 possible keys (written 26!). Far too many to try one by one. Yet it was still broken for centuries, by frequency analysis: in English the letter E is the most common, so the most common letter in a long ciphertext is probably E. The cipher hides which letter is which, but not how often each one appears. The lesson: a huge key space is necessary, but it is not sufficient. The scrambling also has to leave no patterns behind.
What real encryption does
Modern encryption such as AES (the Advanced Encryption Standard, published in 2001 and used for most of the web's traffic and your phone's storage) keeps the one idea that works from Caesar, a shared secret key, and fixes the rest.
| Cipher | Possible keys | Time to try them all at a trillion per second |
|---|---|---|
| Caesar | 25 | far below a billionth of a second |
| Any substitution | about 4 × 1026 | broken by frequency analysis instead |
| AES with a 128-bit key | 2128, about 3.4 × 1038 | about 1019 years |
That last figure is about 780 million times the age of the universe (roughly 13.8 billion years). AES also mixes and re-mixes the data over many rounds so that patterns like letter frequency do not survive. And the key is long and random, not a number between 1 and 25.
One more lesson: the secret is the key, not the method
In 1883 the cryptographer Auguste Kerckhoffs argued that a system should stay secure even if everything about it, except the key, is public. AES follows that rule: its design is published and studied by researchers worldwide, and it is trusted because nobody has found a way to break it without the key. Keeping the method secret and hoping nobody notices is called "security through obscurity", and it tends to fail the moment someone works the method out.
What this means for your own code
Never invent your own encryption for anything that matters. Caesar-style tricks are fun for puzzles and learning, but real data needs a well-tested library implementing a standard like AES, with keys created by that library's secure random generator. If you are ever tempted to hide a password by shifting its letters, or "encoding" it with something that can be undone in 25 steps, remember the box above: anyone who finds the method finds the secret.