The Vigenère cipher explained

The Vigenère cipher is the cipher that beat frequency analysis, and then lost to a cleverer version of it. A short keyword turns one Caesar shift into many, and for three centuries that was enough to defeat every codebreaker who tried. Its story shows the central idea of cryptanalysis: every regularity in a cipher, even a hidden one, is a door.

The tabula recta as bands of shifted alphabets, with the row of the key letter M and the column of the plain letter T lit and the cipher letter F where they meet, above ATTACK over the key LEMON
The tabula recta as bands of shifted alphabets, with the row of the key letter M and the column of the plain letter T lit and the cipher letter F where they meet, above ATTACK over the key LEMON

One shift per letter

A Caesar cipher shifts every letter by the same amount, so it keeps the shape of the language's letter frequencies and falls to frequency analysis at once. The Vigenère cipher uses a keyword instead. Write the key under the message, repeating it as often as needed, and shift each letter by the key letter below it, counting A as 0 and Z as 25: c = (p + k) mod 26. With the key LEMON, ATTACK AT DAWN becomes LXFOPV EF RNHR. The two Ts of ATTACK become X and F, and the two As of DAWN and AT become R and E. Decrypting subtracts the key: p = (c − k) mod 26.

The tabula recta

In 1508 the abbot Johannes Trithemius printed a square of 26 alphabets, each shifted one place further than the one above. To encrypt with it, find the column of the plain letter along the top and the row of the key letter down the side: the letter where they cross is the cipher letter. To decrypt, go along the key letter's row to the cipher letter and read the plain letter at the top of its column. Every row is one Caesar cipher, and the keyword says which row to use for each letter.

Who invented it

The cipher is misnamed. Leon Battista Alberti's cipher disc of about 1467 already switched between alphabets during a message. Giovan Battista Bellaso described the repeating keyword in 1553, and Giovan Battista della Porta wrote about similar ciphers. Blaise de Vigenère's book of 1586 described something stronger, the autokey, in which the keyword only starts the key and the message itself continues it. The simpler keyword cipher was later attributed to him, and by the 19th century it was known as le chiffre indéchiffrable, the indecipherable cipher.

Autokey, Beaufort and the one-time pad

The autokey avoids the fatal repetition: after the primer, each letter is enciphered with a letter of the plaintext, so the key never repeats. Its weakness is different: the key is the message, so guessing a probable word reveals the next stretch of key. The Beaufort cipher, used by the British navy and named after Admiral Francis Beaufort, takes c = (k − p) mod 26; doing the same again gives the plaintext back, so one wheel does both. Taken to its limit, a key of truly random letters as long as the message, used only once, is the one-time pad of Gilbert Vernam and Joseph Mauborgne, which Claude Shannon proved in 1949 to be unbreakable.

Kasiski's examination

A repeating key leaves a trace. Whenever a word recurs in the plaintext at a distance that is a multiple of the key length, it meets the same key letters and produces the same ciphertext. So repeated sequences in the ciphertext, three letters or more, mostly lie a multiple of the key length apart. Charles Babbage used this in the 1850s, apparently to answer a challenge, but never published it; the Prussian officer Friedrich Kasiski published it in 1863. List the spacings between repeats, count how many of them each number divides, and the key length and its multiples stand out above the chance repeats.

Friedman's index of coincidence

William Friedman, the founder of American codebreaking, introduced in 1922 a measure that needs no lucky repeats: the index of coincidence, the probability that two letters taken at random from a text are the same. In English it is about 0.065, because E, T and A are so common; for letters spread evenly it is 1/26 ≈ 0.038. A Vigenère ciphertext mixes several shifts and lies in between. Split it into columns every k letters: when k is the key length, each column is a single Caesar shift and its index rises to the language's value. Friedman also turned the index of the whole text into an estimate of the key length, rough for short texts but quick.

Recovering the key

With the length known, the cipher falls apart into one Caesar cipher per key letter. For each column try all 26 shifts and compare the letter counts of the result with the language's expected counts, for example with the chi-squared statistic, the sum of (observed − expected)² / expected over the letters. The lowest score marks the right shift, and the shifts of the columns spell the key. A column of a few dozen letters is enough. The method fails when the key is nearly as long as the message or never repeats, which is why the one-time pad and the autokey resist it.

Explore it

Open the Vigenère cipher to encrypt and decrypt your own messages on the tabula recta, with the Vigenère, autokey or Beaufort cipher, and watch the row, the column and the cipher letter light up for each step. Then switch to Kasiski examination, the index of coincidence or the column frequencies, play a long example, and watch the key length and the key emerge as more ciphertext arrives, or fail to emerge for a one-time pad.

Frequently asked questions

Is the Vigenère cipher secure today?

No. A computer recovers the key of an ordinary Vigenère ciphertext in a fraction of a second, as long as the message is a few times longer than the key. It remains a classic teaching cipher and a favourite of puzzles, geocaches and escape rooms.

How long must the ciphertext be to crack it?

As a rule of thumb, a few dozen letters for every letter of the key: then every column is long enough for frequency analysis. With a five-letter key, two hundred letters are usually plenty; with a key half as long as the text, the statistics have too little to work with.

What is the difference between the Vigenère and the Caesar cipher?

A Caesar cipher uses one shift for the whole message, so it has only 25 keys and keeps the letter frequencies of the language. The Vigenère cipher uses a different shift for each position of the key, so a key of length m gives 26^m possible keys and flattens the frequencies. A Vigenère cipher with a one-letter key is a Caesar cipher.

Why does a longer key make the cipher stronger?

Each column then holds fewer letters, so its frequencies say less, and repeats of the same plaintext at a multiple of the key length become rarer. When the key is as long as the message and random, every column holds one letter and nothing is left to analyse: that is the one-time pad.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Open the Vigenère cipher