Why Secret Messages Exist
Imagine you have to get a note across a crowded room, and three people along the way are going to read it. You still need your friend on the other side to understand it. Everyone else should see nothing but nonsense.
That is the whole problem cryptography solves. Cryptography is the study of writing and reading secret messages. The tool it uses is called a cipher: a set of rules for scrambling a message so only the right person can unscramble it.
Three words show up on every page of this topic, so learn them now:
- Plaintext is the original message, the part you can read. Example:
MEET AT NOON. - Ciphertext is the scrambled version. Example:
PHHW DW QRRQ. - Key is the secret setting that turns one into the other. Both people need it, and nobody else can have it.
Turning plaintext into ciphertext is called encrypting. Turning it back is decrypting. Figuring out the message when you do not have the key is called cracking or breaking the cipher, and that is what the rest of this page is really about.
Ancient armies needed this badly. A messenger carrying orders on foot could be captured, and a captured message told the enemy exactly where your soldiers were headed. Merchants needed it for prices, and rulers needed it for anything they did not want a rival to read. The messenger was never safe, so the message had to protect itself.
A good cipher stays safe even if your enemy knows exactly how it works. Only the key is supposed to be secret. If your whole plan depends on nobody figuring out the method, you do not have security, you have a lucky streak.
How the Caesar Cipher Works
The Caesar cipher is named after Julius Caesar, the Roman general who used it to protect military orders. It is a substitution cipher, which means every letter gets swapped for a different letter using one steady rule.
The rule is short: pick a number, then slide every letter that many places forward in the alphabet. That number is your key, and it is usually called the shift.
Say the shift is 3. Write the alphabet out, then write it again underneath, moved three places over:
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 Shift 3 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
Now encrypting is just reading down. A becomes D. B becomes E. H becomes K.
Notice what happens at the end of the alphabet. X becomes A, because after Z you wrap around to the front again, like the numbers on a clock rolling past 12. Mathematicians call that wrap "working modulo 26," and 26 is simply the number of letters in the alphabet.
A worked example
Let us encrypt the word HELLO with a shift of 3, one letter at a time.
So HELLO encrypts to KHOOR. To decrypt, your friend does the same thing backwards: shift every letter three places back, and KHOOR turns into HELLO again.
Two details worth remembering. Spaces and punctuation usually stay put, so MEET AT NOON becomes PHHW DW QRRQ with the word breaks intact. And a shift of 0 does nothing at all, so it is a key you should never pick.
This is the most important fact about the Caesar cipher, and it is the crack in the armor. With a shift of 3, every single E in your message becomes an H. Every time. That pattern is exactly what a code breaker goes looking for.
Breaking the Caesar Cipher
The Caesar cipher held up fine when most people could not read. Today it does not last thirty seconds, and there are two separate reasons why.
Reason one: there are only 25 keys
Count the useful shifts. You can shift by 1, 2, 3, all the way up to 25. Shifting by 26 lands you right back where you started, so it does nothing, and neither does 0. That leaves exactly 25 possible keys.
An attack that tries every possible key until one works is called a brute force attack. Twenty five keys is nothing. A person with a pencil can grind through them in a few minutes, and a computer program can test all 25 and print the readable one before you lift your finger off the Enter key.
shift 1 -> VJG EQFG KU TGF
shift 2 -> UIF DPEF JT SFE
shift 3 -> THE CODE IS RED <-- readable English
shift 4 -> SGD BNCD HR QDC
...
25 keys tested in 0.001 seconds
Reason two: letters have fingerprints
Even without brute force, English gives the cipher away. Letters are not used equally often. In ordinary English text, E is the most common letter by a wide margin, followed by letters like T, A, O, I, and N. Letters like Q and Z barely show up at all.
Game shows lean on this. When contestants get a set of free letters before the final puzzle, the letters handed out are the common ones, because those are the letters most likely to be in any English phrase.
Counting how often each letter appears in a message is called frequency analysis, and it wrecks the Caesar cipher. Since every E turns into the same ciphertext letter, the most common letter in the ciphertext is almost certainly a disguised E. Find it, measure how far it sits from E, and you have the key, without guessing at all.
The one situation where this gets shaky is a very short message. In five letters, the counts do not mean much. But give a code breaker a paragraph and the pattern is unmistakable.
Decode this message by hand. It was encrypted with a Caesar shift of 3, so shift each letter three places back: WKLV LV IXQ
Work the first word: W back 3 is T, K back 3 is H, L back 3 is I, V back 3 is S. That gives THIS, and the rest falls out the same way. Notice you only had to decode about four letters before the answer was obvious. That is why short shifts are so fragile.
The Vigenère Cipher: One Key, Many Shifts
The Caesar cipher fails because it uses one shift for the entire message. So here is the fix: use a different shift for every letter.
The Vigenère cipher (say it "vee-zhen-air") does exactly that. Instead of a number, the key is a keyword, and each letter of that keyword stands for its own shift. A means shift 0, B means shift 1, C means shift 2, and so on up to Z, which means shift 25.
You write the keyword above your message, repeating it as many times as you need, and then each column gets shifted by its own amount. It is really just a stack of Caesar ciphers taking turns.
A worked example
Encrypt ATTACK with the keyword KEY. First repeat the keyword to match the length: KEYKEY. Then look up each key letter's shift: K is 10, E is 4, and Y is 24.
Check one column so you trust the rest. The third letter is T, which sits at position 19 in the alphabet if you start counting at 0. The key letter is Y, worth 24. Add them: 19 plus 24 is 43. That is past the end of the alphabet, so subtract 26 and get 17, which is R.
Now look at what happened to the two Ts in ATTACK. The first one became X. The second one became R. Same plaintext letter, two different ciphertext letters, because they landed on different letters of the keyword.
That single fact is what breaks frequency analysis. If E can come out as five different letters, counting letters tells the attacker almost nothing.
Brute force gets much worse for the attacker too. Caesar had 25 keys. A Vigenère keyword can be any length, so a five letter keyword already gives about 12 million combinations, and a ten letter keyword pushes that past 100 trillion. For centuries this cipher was called le chiffre indéchiffrable, French for "the unbreakable cipher."
Try a keyword that is a single letter, like D. Every position gets a shift of 3, and you are back to an ordinary Caesar cipher. Vigenère is not a different idea, it is the same idea used many times over.
Try It: The Cipher Lab
You have now done both ciphers by hand, which is the part that actually teaches you how they work. This is the part where you get to play.
Pick a mode, type a message, and watch the ciphertext change as you go. In Caesar mode, drag the shift slider and watch the bottom alphabet strip physically slide underneath the top one, so you can see every letter's replacement at once. In Vigenère mode, type a keyword and watch the shifts change from letter to letter.
Drag from 0 to 25. A shift of 0 leaves the message exactly as it is.
Cipher wheel: plain alphabet on top, shifted alphabet below
With a shift of 3, A becomes D and Z becomes C. Highlighted columns are the letters your message actually uses.
Letters only. The keyword repeats across your message.
Letter by letter, with the keyword repeating underneath
Two experiments worth running before you move on. First, in Vigenère mode, type a message full of the letter E and watch how many different letters it turns into. Second, set the keyword to a single letter and compare the result to Caesar mode with the matching shift. They match, because a one letter keyword is a Caesar cipher.
Why does the Vigenère cipher defeat simple frequency analysis?
Frequency analysis works by finding the ciphertext letter that shows up most often and calling it E. Vigenère breaks that, because one E can come out as many different letters depending on which keyword letter it lines up with. The letter counts get flattened out, and the easy shortcut disappears.
Even Vigenère Falls
"Unbreakable" lasted about 300 years. The weakness is not the shifting, it is the repeating.
Look back at the ATTACK example with the keyword KEY. Both As came out as K. That was not luck. The keyword is 3 letters long, and those two As sat exactly 3 letters apart in the message, so they landed on the same keyword letter and got the same shift.
Now scale that up. In a long message, a common word like THE will appear many times. Every so often, one of those THEs will line up with the same part of the keyword as an earlier one, and it will encrypt to the exact same chunk of ciphertext. The attacker sees the same three letters repeating in the scrambled text and knows something is going on.
Here is the clever part. Measure the distance between those repeats. If the same chunk shows up at position 20 and again at position 35, the gap is 15, and the keyword length is probably a number that divides evenly into 15, like 3 or 5. Collect a few of these gaps and the possible key lengths narrow down fast. That technique is called the Kasiski examination, published in 1863 by Friedrich Kasiski.
Once you know the keyword is, say, 5 letters long, the whole thing collapses. Take every 1st letter, every 6th letter, every 11th letter, and so on. All of those were shifted by the same amount, which means that group is just a plain Caesar cipher. Run frequency analysis on it and you get one letter of the keyword. Do that five times and you have the entire key.
Notice the pattern in both attacks. Caesar repeats a single shift, so the letters keep their fingerprints. Vigenère repeats a short keyword, so the fingerprints come back on a longer cycle. Any time a cipher does the same thing over and over, someone can eventually find the rhythm.
There is one way to make Vigenère truly unbreakable, and it is not practical. Use a random key that is as long as the message and never reuse it. That is called a one time pad, and it really cannot be cracked, because every possible message of that length is an equally believable answer. The catch is that you have to somehow deliver a key as long as your message, in secret, ahead of time. If you could do that safely, you could have just delivered the message.
From Pen and Paper to Modern Encryption
Everything on this page still lives inside the encryption protecting your messages, your bank app, and the little padlock in your browser's address bar. The ideas did not get replaced, they got much, much bigger.
Three things changed.
- The keyspace exploded. The keyspace is the number of possible keys an attacker would have to try. Caesar has 25. A modern cipher called AES-256 has roughly 1077 keys, a number with 77 digits, which is in the neighborhood of the count of atoms in the observable universe. Brute force stops being slow and starts being impossible.
- Computers took over the arithmetic. Modern ciphers do not shift letters, they scramble the raw bits a file is made of, mixing and re-mixing them through many rounds. No human could do it by hand, and no human has to.
- Patterns got engineered out. Caesar and Vigenère both die from repetition, so modern ciphers are built specifically so that repeated input never produces repeated output. Encrypt the same word twice and you get two completely different results.
What did not change is the shape of the problem. There is still a plaintext, a ciphertext, and a key. The key is still the only secret. And a cipher is still only as good as the hardest attack anyone can think of, which is why cryptographers spend their careers trying to break the very ciphers they rely on.
Caesar and Vigenère are small enough to do with a pencil, which means you can actually see the mechanism instead of trusting a black box. Every attack idea you met here (brute force, frequency analysis, hunting for repeated patterns) is still exactly how people attack real systems today. Only the scale changed.
Quick Review
The whole page in seven lines:
- A cipher turns plaintext into ciphertext using a key. Only the key is secret.
- The Caesar cipher slides every letter forward by the same amount, wrapping around from Z back to A.
- Caesar has only 25 keys, so a brute force attack tries them all in a fraction of a second.
- Frequency analysis beats it too: the most common ciphertext letter is almost always a disguised
E. - The Vigenère cipher uses a repeating keyword, giving each position its own shift, so one plaintext letter can become many different ciphertext letters.
- Vigenère falls to the Kasiski examination: repeated chunks in the ciphertext reveal the keyword length, and each group then breaks like an ordinary Caesar cipher.
- Modern encryption keeps the same three pieces but uses an enormous keyspace and deliberately erases repeating patterns.
A message is encrypted with Vigenère, and an attacker spots the chunk QLM at position 12 and again at position 30. What is the most useful thing that tells them?
That is the Kasiski examination. The gap is 30 minus 12, which is 18, so likely keyword lengths are factors of 18 such as 2, 3, 6, or 9. A couple more repeats narrow it to one answer, and from there each group of letters is just a Caesar cipher waiting to be counted.
Next time you hear that something is "encrypted," you know what that actually means: somebody chose a key, scrambled the message with it, and is betting that nobody else can find the pattern. Two thousand years later, that is still the whole game.