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

Security · 5 min read ·

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.

Two rows of the alphabet, the lower row shifted by three letters, beside the title Caesar cipher: why 25 guesses break it

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.

CipherPossible keysTime to try them all at a trillion per second
Caesar25far below a billionth of a second
Any substitutionabout 4 × 1026broken by frequency analysis instead
AES with a 128-bit key2128, about 3.4 × 1038about 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.

$ ls security/

see all →
security/cross-site-scripting-how-a-comment-box-can-run-someone-else-s-code.md Cross-site scripting: how a comment box can run someone else's code A comment box looks harmless until someone types a tiny program instead of a comment. This article explains cross-site scripting, or XSS… 5 min read · 28 Oct security/two-factor-codes-how-your-phone-and-a-website-agree-on-six-digits-with.md Two-factor codes: how your phone and a website agree on six digits without talking Your authenticator app shows six digits and the website expects the same six, yet the two never talk. How? Both sides start from the same… 5 min read · 24 Oct security/sql-injection-how-a-quote-mark-in-a-login-form-can-rewrite-the-databas.md SQL injection: how a quote mark in a login form can rewrite the database's question A login form builds a database question out of the words you type. If it glues your text straight into the question, your text can rewrite… 5 min read · 21 Oct security/password-strength-why-length-beats-a-capital-letter-and-a-symbol.md Password strength: why length beats a capital letter and a symbol Everyone says passwords need a capital, a number and a symbol, and also that they should be long. Which one actually matters more? The… 6 min read · 18 Oct security/password-hashing-why-a-website-never-stores-your-password-and-what-a-s.md Password hashing: why a website never stores your password, and what a salt changes Websites should never store your password, only a hash made from it. But two users with the same password get the same hash, and a stolen… 5 min read · 15 Oct security/two-factor-codes-how-your-phone-and-a-website-agree-on-6-digits.md Two-factor codes: how your phone and a website agree on 6 digits Your phone shows a 6-digit code that changes every 30 seconds, yet it never talks to the website. How do both sides agree on the number?… 5 min read · 9 Oct