Skip to content
CryptographyInternals

How to Crack a Caesar Cipher and Break Vigenère

A hands-on cryptanalysis tutorial: crack a Caesar cipher by brute force and frequency analysis, then break Vigenère with Kasiski to recover the key LEMON.

Published on 11 min read

To crack a Caesar cipher, try all 25 shifts and keep the one that reads as language, or count the ciphertext letters and match the most frequent one to E: the gap between them is the key. To break a Vigenère cipher, first find the key length with the Kasiski examination (repeated fragments sit at distances that are multiples of the key length), then cut the ciphertext into that many columns and break each column as a Caesar cipher with frequency analysis. Below, we do both on real ciphertexts, step by step, and every number comes from the same engine that powers the interactive pages on this site.

The message we will attack

Suetonius records that Julius Caesar protected his letters by replacing each letter with the one three places further down the alphabet. Any shift works the same way, so let's use 7 and encrypt a sentence an attacker might plausibly intercept:

Plaintext:  Meet me at the east gate tonight, bring the seven letters and tell no one.
Shift 7:    Tlla tl ha aol lhza nhal avupnoa, iypun aol zlclu slaalyz huk alss uv vul.

Every letter moved seven places: M became T, E became L, and the alphabet wraps around, so the T of "the" became A. We leave spaces in place for readability; the attacks below do not need them.

You can turn the shift wheel yourself on the interactive Caesar cipher page, then scroll to watch the page run the same attacks described below.

Attack 1: try all 25 shifts

The whole key of a Caesar cipher is one number between 1 and 25 (a shift of 26 gives back the original). That is not a key space, it is a short list. Decrypt with every candidate and read:

 1 Skkz sk gz znk kgyz mgzk zutomnz, hxotm znk ykbkt rkzzkxy gtj zkrr tu utk.
 2 Rjjy rj fy ymj jfxy lfyj ytsnlmy, gwnsl ymj xjajs qjyyjwx fsi yjqq st tsj.
 3 Qiix qi ex xli iewx kexi xsrmklx, fvmrk xli wizir pixxivw erh xipp rs sri.
 4 Phhw ph dw wkh hdvw jdwh wrqljkw, eulqj wkh vhyhq ohwwhuv dqg whoo qr rqh.
 5 Oggv og cv vjg gcuv icvg vqpkijv, dtkpi vjg ugxgp ngvvgtu cpf vgnn pq qpg.
 6 Nffu nf bu uif fbtu hbuf upojhiu, csjoh uif tfwfo mfuufst boe ufmm op pof.
 7 Meet me at the east gate tonight, bring the seven letters and tell no one.
 8 Ldds ld zs sgd dzrs fzsd snmhfgs, aqhmf sgd rdudm kdssdqr zmc sdkk mn nmd.
 9 Kccr kc yr rfc cyqr eyrc rmlgefr, zpgle rfc qctcl jcrrcpq ylb rcjj lm mlc.
10 Jbbq jb xq qeb bxpq dxqb qlkfdeq, yofkd qeb pbsbk ibqqbop xka qbii kl lkb.
11 Iaap ia wp pda awop cwpa pkjecdp, xnejc pda oaraj happano wjz pahh jk kja.
12 Hzzo hz vo ocz zvno bvoz ojidbco, wmdib ocz nzqzi gzoozmn viy ozgg ij jiz.
13 Gyyn gy un nby yumn auny nihcabn, vlcha nby mypyh fynnylm uhx nyff hi ihy.
14 Fxxm fx tm max xtlm ztmx mhgbzam, ukbgz max lxoxg exmmxkl tgw mxee gh hgx.
15 Ewwl ew sl lzw wskl yslw lgfayzl, tjafy lzw kwnwf dwllwjk sfv lwdd fg gfw.
16 Dvvk dv rk kyv vrjk xrkv kfezxyk, sizex kyv jvmve cvkkvij reu kvcc ef fev.
17 Cuuj cu qj jxu uqij wqju jedywxj, rhydw jxu iulud bujjuhi qdt jubb de edu.
18 Btti bt pi iwt tphi vpit idcxvwi, qgxcv iwt htktc atiitgh pcs itaa cd dct.
19 Assh as oh hvs sogh uohs hcbwuvh, pfwbu hvs gsjsb zshhsfg obr hszz bc cbs.
20 Zrrg zr ng gur rnfg tngr gbavtug, oevat gur frira yrggref naq gryy ab bar.
21 Yqqf yq mf ftq qmef smfq fazustf, nduzs ftq eqhqz xqffqde mzp fqxx za azq.
22 Xppe xp le esp plde rlep ezytrse, mctyr esp dpgpy wpeepcd lyo epww yz zyp.
23 Wood wo kd dro okcd qkdo dyxsqrd, lbsxq dro cofox voddobc kxn dovv xy yxo.
24 Vnnc vn jc cqn njbc pjcn cxwrpqc, karwp cqn bnenw unccnab jwm cnuu wx xwn.
25 Ummb um ib bpm miab oibm bwvqopb, jzqvo bpm amdmv tmbbmza ivl bmtt vw wvm.

Line 7 jumps out. Nothing else comes close to English: a line like 23 starts with a real word ("Wood") and then collapses into noise. That is the whole attack. With a computer you do not even need to read the lines: give each one a score based on how common its letters are in English and keep the best. Our scoring function picks shift 7 on its own.

Brute force works only because the key space is tiny, which is why the second attack matters more.

Attack 2: frequency analysis

Languages are not random. In English, E is by far the most common letter, followed by T, A and O. A Caesar cipher moves every letter by the same amount, so it moves the frequency profile too, but it does not change its shape. Whatever letter E became is still the most common one.

Here is the reference profile used on this site (Robert Lewand, Cryptological Mathematics, MAA 2000, as reproduced in Wikipedia's "Letter frequency" article), next to what we actually count in our 58-letter ciphertext:

RankEnglish letterFrequency in EnglishCiphertext letterCount in our ciphertext
1E12.7%L13
2T9.1%A11
3A8.2%U6
4O7.5%H4
5I7.0%N3

The most frequent ciphertext letter is L. If L stands for E, the shift is the distance from E to L: E is letter 4 (counting from A = 0) and L is letter 11, so 11 minus 4 gives 7. Decrypt with 7 and the sentence appears. The second line of the table confirms it: A minus 7 wraps around to T, the second most common English letter.

Two caveats. First, short texts are noisy: a ten-letter message can easily have T or A on top instead of E, and then you try the next candidate. Second, matching only the single top letter throws away most of the information. Scoring all 26 letters against the full table, as the brute-force scorer above does, is much more robust. We will need that robustness in a minute.

Scramble the whole alphabet: 26! keys, same weakness

The obvious fix is to stop shifting and scramble instead: a monoalphabetic substitution cipher maps each letter to any other letter, using a full permutation of the alphabet as the key. The number of possible keys is 26 factorial:

26! = 403,291,461,126,605,635,584,000,000  (about 4 × 10^26)

Nobody is going to try them all. Yet this cipher fell more than a thousand years ago. The Arab scholar al-Kindi showed in the 9th century how to break it by frequency analysis, because a substitution still sends every E to the same symbol.

Take a keyword alphabet built from KINDI (the duplicate I is dropped, then the unused letters follow in order), which gives KINDABCEFGHJLMOPQRSTUVWXYZ. Our engine encrypts:

Plaintext:  Every secret message can be read by someone patient enough to count the letters.
Ciphertext: Avary sanrat lasskca nkm ia rakd iy solaoma pktfamt amouce to noumt tea jattars.

The ciphertext letter A appears 15 times, almost twice as often as the next one (T, 8 times). So A is almost certainly E. Write E above every A: the first word becomes E_E__, and the three-letter word tea becomes __E, which is almost certainly THE. That hands you T and H for free, and the rest unravels like a crossword: each confirmed letter makes the next guess easier. A huge key space protects nothing if the ciphertext leaks the structure of the plaintext. The substitution cipher page lets you type your own keyword, build the cipher alphabet from it, and watch the letter counts betray it.

Vigenère: "le chiffre indéchiffrable"

Giovan Battista Bellaso described a stronger idea in 1553, later credited to Blaise de Vigenère: use several Caesar shifts in turn, driven by a keyword. With the key LEMON, the first letter is shifted by L (11), the second by E (4), the third by M (12), the fourth by O (14), the fifth by N (13), and then the key starts over. The same plaintext letter now encrypts differently depending on its position, which flattens the frequency profile. For three centuries the cipher was nicknamed "le chiffre indéchiffrable", the indecipherable cipher.

Here is a 452-letter field order encrypted with LEMON by the site's engine:

Plaintext:
The enemy will cross the river at dawn near the old mill. Send every available rider to the eastern bridge and hold the bridge until the second signal. The enemy expects the river crossing to be undefended, so the riders must stay hidden in the trees until the enemy reaches the middle of the bridge. When the second signal comes, attack from both sides of the river and take the enemy officers alive. Do not burn the bridge, because we need the bridge for the march to the capital next week. Report to the general at the old mill when the river crossing is secure.

Ciphertext (key LEMON):
Elq sapqk kvwp ofbdw fvr cmhse lx pojy rqoe elq cyo quzy. Dizr rgidm ngeuznmpq fvoid hb elq sndxqfa mvurtp ezr uzpp hup fdwqri gbgtp fvr diocao wuualp. Fvr prqal pbbspew fvr cmhse nvagftrs hb mi gbqpjqbqph, ec gsi dwqpve ahdx ehnj lurqpr ub gsi ffrpw gbgtp fvr prqal cimqupw fvr xmpryp sr hup fdwqri. Ivry xts fpgabq dmsbnw gaard, efhnno rfbx fahu dmpsf zj fvr cmhse lrp hnvi fvr prqal zjrwppve oytzq. Rb ysf phcr fvr mvurtp, fqqnfwq kr yiqr gsi nfvokq tbc xts zlvov gz xts pltuhnw rqlg hiqy. Eptafg es fvr rizselp mh gsi azq xmxz jsiz hup vujrc gdcfdmzu vd wqqhci.

Look at "the": it comes out as Elq, elq, fvr, gsi and xts depending on where it falls under the key. Single-letter counting no longer works on the whole text. The attack has to find the key length first.

Step 1: the Kasiski examination finds the key length

Charles Babbage and later Friedrich Kasiski (1863) noticed the flaw: the key repeats. When the same plaintext fragment happens to line up with the same part of the key twice, it produces the same ciphertext fragment twice. The distance between those two occurrences must then be a multiple of the key length.

We strip everything but the 452 letters and search for fragments of 3 to 6 letters that repeat. The engine finds 26 such repeats. A selection, with positions counted from 0:

Repeated fragmentPositionsDistanceFactors
WFVRCM16, 1361202 × 2 × 2 × 3 × 5
CMHSEL20, 2902702 × 3 × 3 × 3 × 5
HUPFDW93, 2331402 × 2 × 5 × 7
GBGTPF102, 2021002 × 2 × 5 × 5
FVRPRQ122, 207, 302855 × 17
MVURTP80, 3352553 × 5 × 17
ELQS0, 70702 × 5 × 7
GSI169, 194, 354, 414255 × 5
IGB101, 156555 × 11
ALP119, 12893 × 3
SFP248, 326782 × 3 × 13

The factor 5 is everywhere. Across all 26 repeats, 5 divides 24 of the distances, more than any other candidate length (2 divides 16, 10 divides 15, 4 divides 13). The two exceptions, ALP and SFP, are coincidences: different plaintext under different key letters that happened to produce the same three letters. That is normal, and it is why you count votes instead of trusting a single repeat. Our key length is 5.

Step 2: five columns, five Caesar ciphers

If the key has 5 letters, then letters 0, 5, 10, 15... were all shifted by the first key letter, letters 1, 6, 11... by the second, and so on. Split the ciphertext into five columns and each column is an ordinary Caesar cipher with about 90 letters, plenty for frequency analysis.

ColumnLettersMost frequentNaive guess (top = E)Best full-profile score
191P (20), D (10), C (8)LL
291I (18), R (9), M (8)EE
390Q (18), F (13), U (9)MM
490V (12), H (11), S (11)RO
590R (16), G (9), N (9)NN

Columns 1, 2, 3 and 5 give up their key letter to the simplest rule: P is 11 places after E, so the shift is 11, which is L; I is 4 after E, giving E; and so on.

Column 4 is the instructive one. Its top three letters are nearly tied (12, 11, 11), and the naive "top letter is E" rule says R. The full-profile score, which tries all 26 shifts and rewards the one whose decrypted letters are most English-like overall, chooses O instead. Check it by hand: a shift of 14 turns V into H, H into T and S into E. Three common English letters at once is far more convincing than one. This is exactly the robustness we mentioned in the Caesar section.

Reading the five winners in order gives L, E, M, O, N. Decrypting with LEMON returns "The enemy will cross the river at dawn near the old mill..." The indecipherable cipher is open.

The interactive Vigenère page shows this attack running: watch Kasiski highlight the repeated fragments, vote for a key length, and see the five columns spell out LEMON letter by letter.

What made this attack possible

Three things, all worth remembering because modern ciphers are designed against each one:

  • A short, repeating key. A 5-letter key over 452 letters is reused more than 90 times. The longer the message relative to the key, the more repeats Kasiski finds.
  • Letter-level structure. Each ciphertext letter depends on exactly one plaintext letter and one key letter, so the language's statistics pass through, just split across columns.
  • Repetitive plaintext. Orders repeat words like "the bridge" and "the enemy", which multiplies the repeats.

Why modern ciphers do not fall to these attacks

Everything above exploits one fact: classical ciphers let the statistics of the plaintext leak into the ciphertext, and their key spaces are either tiny (Caesar) or attackable piece by piece (Vigenère, one column at a time).

A modern block cipher like AES closes both doors.

  • No piece-by-piece attack. AES encrypts 128-bit blocks through many rounds of substitution and mixing. Changing one bit of the input or of the key changes roughly half the output bits. Claude Shannon called this property diffusion: the structure of the plaintext is spread across the whole block, so there is no letter frequency left to count and no column that depends on only one part of the key.
  • No useful brute force. A 128-bit key gives 2^128, about 3.4 × 10^38, possibilities. That is nearly a trillion times more than the 4 × 10^26 alphabets of the substitution cipher, and unlike that cipher, there is no shortcut that lets you solve it one letter at a time.

So the lesson of al-Kindi, Babbage and Kasiski is not "use a bigger key". The substitution cipher already had a big key. The lesson is that a cipher must hide the patterns of its input, and that is what AES is built to do. See it for yourself on the AES page, where you can follow a block through its rounds and watch a single flipped bit spread across the output.

From 25 keys to 2^128

Caesar fell to brute force in seconds because it has 25 keys. The brute-force page runs that same attack against keys of growing size and shows where it stops being an attack at all: at 128 bits, even the hash rate of the entire Bitcoin network would need billions of years. Start there, then go back to the Caesar wheel and break a message of your own.