By Tu KS MS
1. Introduction
For around three hundred years, the Vigenère Cipher was known as le chiffre indéchiffrable: the indecipherable cipher. First described by Giovan Battista Bellaso in 1553, and later credited to Blaise de Vigenère by mistake, it was finally broken in the 19th century by Charles Babbage and, in 1863, by Friedrich Kasiski. In this article, we define the cipher precisely and show how Kasiski’s test and William Friedman’s index of coincidence can be used to break it.
2. Mathematical Prerequisites
2.1 Cryptosystem
Below is the formal definition of a cryptosystem that we will use throughout the text. The reason we define a cryptosystem so precisely is because it allows us to compare different ciphers, find common properties they share, or perhaps, as what mathematicians like to do, prove results generally. It also describes the necessary ingredients to contriving your own cryptosystems.
Definition 2.1 (Cryptosystem). A cryptosystem is a 5-tuple satisfying the following conditions:
- is the finite set of all plaintexts, called the plaintext space.
- is the finite set of all ciphertexts, called the ciphertext space.
- is the finite set of all possible keys, called the keyspace.
- For each key , there is an encryption rule and a corresponding decryption rule . Each and are functions such that for every plaintext element .
Remark. The fundamental property of a cryptosystem is rule 4. It is neat that cryptosystems are determined by functions. The existence of the decryption rule relies on the corresponding encryption rule to be invertible.
2.2 Modular arithmetic
Throughout this text, given positive integer , let .
Addition in can be explicitly written, given integers , as , which is the remainder of divided by . The remainder of any integer divided by is always in . Subtraction is defined in the same way, as , taking the remainder in even when is negative.
2.3 Numerical representation of the alphabet
It is difficult to work with letters mathematically. However, we can easily establish a one-to-one correspondence between an -letter alphabet and . Take the 26-letter English alphabet, for example:
| A | B | C | D | E | F | G | H | I | J | K | L | M |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
Remark. We will be focusing on the cryptanalysis of encrypted English messages, which means that we will be working in . It is clear, though, that this one-to-one correspondence between the alphabet and numbers can be extended to other languages.
3. Two Classical Ciphers
Definition 3.1 (Shift (Caesar) cipher). Let . For any key , define
Note: ,
The Shift Cipher is one of the most notable ciphers (since it is easily understood), where the plaintext letters are simply “displaced” in their alphabet. Although distinct from the Vigenère Cipher, it will turn out to be useful in its cryptanalysis.
For example, the plaintext irlylovecryptography with the shift (key) of 3 gives the ciphertext
[ hbox, size=fbox, varwidth upper, center, fontupper=, colback=white, colframe=black, boxrule=0.5pt, arc=2pt]
LUOBORYHFUBSWRJUDSKB
Definition 3.2 (Vigenère cipher). Let be a positive integer. Let
For any key , define
where all results are reduced modulo 26.
Note: ,
In words, the Vigenère Cipher takes an -letter string as plaintext input and encrypts it by adding the plaintext with the key under . This produces an -letter ciphertext, which can in turn be decrypted into the plaintext by subtracting the same key from it. A plaintext longer than letters is split into blocks of letters, each encrypted with the same key, so the key repeats periodically.
For example, the plaintext irlylovecryptography with the key math gives the ciphertext below; checking this is left as an exercise for the reader.
[ hbox, size=fbox, varwidth upper, center, fontupper=, colback=white, colframe=black, boxrule=0.5pt, arc=2pt]
UREFXOOLORRWFOZYMPAF
4. Cryptanalysis of the Vigenère Cipher
The first step in breaking the Vigenère Cipher is often to determine the keylength . We describe two techniques that will allow us to do this: One is called the Kasiski test and the other uses the index of coincidence.
4.1 Kasiski test
The Kasiski test is based on the observation that whenever the distance between identical segments of plaintext is a multiple of , they will encrypt to the same segments of ciphertext, as the key repeats periodically.
For a rather large chunk of text, it is likely the case that this will happen for three-letter segments called trigrams, because trigrams like THE, ING, AND are statistically common in the English language.
If the ciphertext contains identical trigrams, there is a good chance they correspond to identical plaintext trigrams as well. This happens when the same segments of the key are used to encrypt the plaintext trigrams. In light of this, we can record the distance between pairwise identical ciphertext trigrams . The distance is likely a multiple of , so taking the greatest common factor of will likely give or a multiple of . This reduces the workload of exhaustively searching for the keylength , which, of course, is easily impractical by hand.
We summarize the Kasiski test below:
Technique (Kasiski test). Given a piece of ciphertext, if it contains identical trigrams that appear pairwise with distances , then it is likely that
That is, the keylength is a likely factor of .
Remark. Note that if the ciphertext contains any -gram for , then we can apply this test. However, this test has severe limitations (note the emphasis on “likely”). It is important to recognize that the Kasiski test provides very promising information about when it works, but I have encountered many instances where the ciphertext either did not contain identical trigrams or produced false values of by coincidence.
The Kasiski test is a good starting point, but it does not always work, and even when it does, we still have to find the key itself.
4.2 Index of Coincidence
This is where the index of coincidence comes in: it gives a more reliable way to find the keylength, and then the key itself.
Definition 4.1 (Index of coincidence). Suppose is an -letter string. The index of coincidence (IOC) of , denoted by , is defined as the probability that two elements of , chosen at random from two different positions, are identical.
For any text with frequencies of A, …, Z denoted by , respectively, we have
Furthermore, it is well known that some English letters occur more frequently in literature than others, captured in their relative frequencies, generated by frequency analysis of many English texts. Hence, for an “infinitely” long English string, if the relative frequencies of A, …, Z are denoted by , respectively, then we can compute its expected IOC:
To use the IOC for cryptanalysis of the Vigenère Cipher, we exploit the fact that the key repeats periodically every -letters. Therefore, if we divide the entire ciphertext into -letter sub-ciphertexts and arrange them in rows, the string formed by grouping letters in one column will be encrypted using the same key, equivalent to a Shift Cipher. We thus obtain lots of Shift Cipher encrypted columns.
Determining the keylength
Suppose we have a ciphertext string . Also suppose that we guess a keylength . From the procedure outlined above, we divide into of sub-ciphertexts , each corresponding to a unique key from the key tuple:
Note: if the ciphertext is written in rows of letters, each is a column. The original ciphertext is recovered by reading the rows in order, taking one letter from each in turn.
If the keylength we used is correct, for each sub-ciphertext , since it is encrypted using the Shift Cipher, will be close to the expected IOC of English language. This is because the individual terms are simply approximate permutations of the terms.
However, if the keylength we used is incorrect, the sub-ciphertext strings will be encrypted using different key values and hence appear statistically much more random. A uniformly random string has IOC:
which is considerably different from 0.065.
We summarize this test below:
Technique (IOC test for keylength ). Let be a positive integer. Divide the ciphertext into sub-ciphertexts:
For each , if
then is very likely the correct value. Otherwise, is very likely incorrect.
If is the correct keylength, its multiples will also pass this test, so we take the smallest that does.
Remark. This test is much more reliable than the Kasiski test in the sense that we can always statistically analyze a “long enough” ciphertext string this way. Numbers are also much easier to compare. If you implement this in code, you might be surprised how revealing this test is.
Determining the key
We now introduce a “displaced” version of the IOC that will allow us to determine the key value used to encrypt this sub-ciphertext.
Let denote the frequencies of A, …, Z in , respectively. If has letters, then the relative frequencies of A, …, Z are respectively.
We define the quantity that I will call the “displaced” IOC (DIOC); this is not a standard term, but it is a helpful name:
To determine the key value from , we simply traverse through all values of . The largest value of , which should be close to the expected value of 0.065, indicates the correct shift, and then . This is because the relative frequency distribution of (addition reduced modulo 26)
now corresponds to the natural relative frequency distribution of the English language, .
Thus, is nearly the expected IOC of English language, which is 0.065.
On the other hand, if we use the wrong , then the resultant will be considerably below 0.065. We provide an informal justification of this below, using a result known as the rearrangement inequality.
Proof. Let be a probability distribution. Let be a permutation of probability distribution .
We claim that
is maximized when .
Consider an arbitrary sum . We claim we can do at least as well by swapping two values “out of order”.
More precisely, if for , then swap and . Name the resultant distribution as
Then
This means each swap can only ever increase the resultant sum. By performing this kind of swap we ultimately terminate at the final state
Applying this in context: the correct shift lines up the frequencies of with in the right order, giving the largest value, close to . Any other shift lines them up in a different order, and so can only give a smaller value; in practice it is usually much closer to 0.038.
We summarize the IOC test below:
Technique (IOC test for key ). Suppose we know the keylength .
Divide the ciphertext into sub-ciphertexts:
For each sub-ciphertext of length , find the corresponding key value by choosing the that makes the DIOC largest, close to the expected value 0.065:
We eventually obtain as the key.
Unlike the Kasiski test, this test almost always works and can be easily implemented as code. It is also pretty efficient, for and are typically small.
5. Conclusion
The Vigenère Cipher resisted attack for centuries, yet its weakness is simple: because the key repeats, the ciphertext splits into Shift Ciphers, each of which still carries the letter frequencies of English. Once we know this, the Kasiski test and the index of coincidence let us find first the keylength and then the key. Modern ciphers are designed precisely so that no statistical pattern like this survives encryption.
6. Challenge
Try to cryptanalyse this quote from a famous mathematician using the techniques in this article (without AI). It was encrypted with the Vigenère Cipher, with all spaces and punctuation removed:
RMMTUYVAGZCUAAFRKRRPMIANNRBIABORNRSNDAWEEIOPNKTQRAMRFUZSBAGNNRAJADEZIAECVRYAAYWTGYAZTUYRRFZTUSOYLAHJEFHRSJRRDAPEJCCHVUEMSNGJTUVMMTVWRAAFNFHRICHRIHMNQBJSAFMMTRLRAYKOIOEEFIGYBGTVXNAFRNPSBBRSCRTFEEHBAEVLUKRFHTBCAETYIWGRISUNPYRDRRSIENLUEFJWUTUNRMRKHMNJIADFKHQMNNQEZRTUCVUWSCRTFEEHBLVBEFHRJJIAKEDSBLCHRGOQTFGDSGSENENOCISLLFHRCMENJLUKRNQEPFLAUEMXRGYEIOEXBMHJTRIGNXGRKHQRVHJHNIMANVIDSJRYNENOCYVJTTESCASGKEETGBNRRZSZOCYAMNEEZTCFJCRZNFHRQXRYUFARHAUYZRTTEZUCIPJ
Have a go before opening the solution.
Show solution
Key length. Splitting the ciphertext into columns gives an index of coincidence between about 0.058 and 0.103 in every column, close to the 0.065 of English. No shorter key length gets every column that high.
Key. Maximising the displaced index of coincidence for each column gives the shifts 17, 0, 12, 0, 13, 20, 9, 0, 13, which spell the key RAMANUJAN.
Plaintext. Subtracting the key and putting back the spaces and punctuation gives a passage from G. H. Hardy’s A Mathematician’s Apology (1940):
“A mathematician, like a painter or a poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas. A mathematician, on the other hand, has no material to work with but ideas, and so his patterns are likely to last longer, since ideas wear less with time than words. The mathematician’s patterns, like the painter’s or the poet’s, must be beautiful; the ideas, like the colours or the words, must fit together in a harmonious way. Beauty is the first test: there is no permanent place in the world for ugly mathematics.”
Fittingly, Hardy was the mathematician who brought Ramanujan to Cambridge in 1914.
References
“Cryptosystem.” Wikipedia, Wikimedia Foundation. https://en.wikipedia.org/wiki/Cryptosystem.
“Letter Frequency.” Wikipedia, Wikimedia Foundation. https://en.wikipedia.org/wiki/Letter_frequency.
Lyons, James. “English Letter Frequencies.” Practical Cryptography. http://practicalcryptography.com/cryptanalysis/letter-frequencies-various-languages/english-letter-frequencies/.
“Modular Arithmetic.” Wikipedia, Wikimedia Foundation. https://en.wikipedia.org/wiki/Modular_arithmetic.
Stinson, Douglas, and Maura Paterson. Cryptography: Theory and Practice. 4th ed., Taylor & Francis Group, 2018.
“Vigenère Cipher.” Wikipedia, Wikimedia Foundation. https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher.





Leave a Reply