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 (𝒫,𝒞,𝒦,ℰ,𝒟)(\mathcal{P}, \mathcal{C}, \mathcal{K}, \mathcal{E}, \mathcal{D}) satisfying the following conditions:

  1. 𝒫\mathcal{P} is the finite set of all plaintexts, called the plaintext space.
  2. 𝒞\mathcal{C} is the finite set of all ciphertexts, called the ciphertext space.
  3. 𝒦\mathcal{K} is the finite set of all possible keys, called the keyspace.
  4. For each key K∈𝒦K \in \mathcal{K}, there is an encryption rule eK∈ℰe_K \in \mathcal{E} and a corresponding decryption rule dK∈𝒟d_K \in \mathcal{D}. Each eK:𝒫→𝒞e_K: \mathcal{P} \to \mathcal{C} and dK:𝒞→𝒫d_K: \mathcal{C} \to \mathcal{P} are functions such that dK(eK(x))=xd_K(e_K(x)) = x for every plaintext element x∈𝒫x \in \mathcal{P}.

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 mm, let ℤm={0,1,…,m−1}\mathbb{Z}_{m} = \{0, 1, \ldots, m – 1\}.

Addition in ℤm\mathbb{Z}_{m} can be explicitly written, given integers a,b∈ℤma, b \in \mathbb{Z}_m, as (a+b)modm(a + b) \bmod m, which is the remainder of (a+b)(a + b) divided by mm. The remainder of any integer divided by mm is always in ℤm\mathbb{Z}_m. Subtraction is defined in the same way, as (a−b)modm(a – b) \bmod m, taking the remainder in ℤm\mathbb{Z}_m even when a−ba – b 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 mm-letter alphabet and ℤm\mathbb{Z}_m. Take the 26-letter English alphabet, for example:

ABCDEFGHIJKLM
0123456789101112
NOPQRSTUVWXYZ
13141516171819202122232425

Remark. We will be focusing on the cryptanalysis of encrypted English messages, which means that we will be working in ℤ26\mathbb{Z}_{26}. 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 𝒫=𝒞=𝒦=ℤ26\mathcal{P} = \mathcal{C} = \mathcal{K} = \mathbb{Z}_{26}. For any key K∈𝒦K \in \mathcal{K}, define

eK(x)=(x+K)mod26e_K(x) = (x + K) \bmod 26
dK(y)=(y−K)mod26d_K(y) = (y – K) \bmod 26

Note: x∈𝒫x \in \mathcal{P}, y∈𝒞y \in \mathcal{C}

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 mm be a positive integer. Let 𝒫=𝒞=𝒦=(ℤ26)m\mathcal{P} = \mathcal{C} = \mathcal{K} = (\mathbb{Z}_{26})^m

For any key K=(k1,…,km)∈𝒦K = (k_1, \ldots, k_m) \in \mathcal{K}, define

eK(x1,…,xm)=(x1+k1,…,xm+km)e_K(x_1, \ldots, x_m) = (x_1 + k_1, \ldots, x_m + k_m)
dK(y1,…,ym)=(y1−k1,…,ym−km)d_K(y_1, \ldots, y_m) = (y_1 – k_1, \ldots, y_m – k_m)

where all results are reduced modulo 26.

Note: (x1,…,xm)∈𝒫(x_1, \ldots, x_m) \in \mathcal{P}, (y1,…,ym)∈𝒞(y_1, \ldots, y_m) \in \mathcal{C}

In words, the Vigenère Cipher takes an mm-letter string as plaintext input and encrypts it by adding the plaintext with the key under ℤ26\mathbb{Z}_{26}. This produces an mm-letter ciphertext, which can in turn be decrypted into the plaintext by subtracting the same key from it. A plaintext longer than mm letters is split into blocks of mm 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 mm. 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 mm, 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 δ1,…,δn\delta_1, \ldots, \delta_n. The distance δi\delta_i is likely a multiple of mm, so taking the greatest common factor of δ1,…,δn\delta_1, \ldots, \delta_n will likely give mm or a multiple of mm. This reduces the workload of exhaustively searching for the keylength mm, 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 δ1,…,δn\delta_1, \ldots, \delta_n, then it is likely that

m|gcd⁡(δ1,…,δn)m \mid \gcd (\delta_1, \ldots, \delta_n)

That is, the keylength mm is a likely factor of gcd⁡(δ1,…,δn)\gcd (\delta_1, \ldots, \delta_n).

Remark. Note that if the ciphertext contains any nn-gram for n≥3n \geq 3, 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 mm when it works, but I have encountered many instances where the ciphertext either did not contain identical trigrams or produced false values of δ\delta 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 𝒙=x1…xn\boldsymbol{x} = x_1 \ldots x_n is an nn-letter string. The index of coincidence (IOC) of 𝒙\boldsymbol{x}, denoted by Ic(𝒙)I_c(\boldsymbol{x}), is defined as the probability that two elements of 𝒙\boldsymbol{x}, chosen at random from two different positions, are identical.

For any text 𝒙=x1…xn\boldsymbol{x} = x_1 \ldots x_n with frequencies of A, …, Z denoted by f0,…,f25f_0, \ldots, f_{25}, respectively, we have

Ic(𝒙)=∑i=025(fi2)(n2)=∑i=025fi(fi−1)n(n−1)I_c(\boldsymbol{x}) = \frac{\sum_{i=0}^{25} \binom{f_i}{2}}{\binom{n}{2}} = \frac{\sum_{i=0}^{25} f_i (f_i – 1)}{n (n – 1)}

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 p0,…,p25p_0, \ldots, p_{25}, respectively, then we can compute its expected IOC:

∑i=025pi2≈0.065\sum_{i=0}^{25} p_i^2 \approx 0.065

To use the IOC for cryptanalysis of the Vigenère Cipher, we exploit the fact that the key repeats periodically every mm-letters. Therefore, if we divide the entire ciphertext into mm-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 mm lots of Shift Cipher encrypted columns.

Determining the keylength

Suppose we have a ciphertext string 𝒚=y1…yn\boldsymbol{y} = y_1 \ldots y_n. Also suppose that we guess a keylength mm. From the procedure outlined above, we divide 𝒚\boldsymbol{y} into mm of sub-ciphertexts 𝒚1,…,𝒚m\boldsymbol{y}_1, \ldots, \boldsymbol{y}_m, each corresponding to a unique key from the key tuple:

𝒚i=yiym+iy2m+i…for i=1,…,m.\boldsymbol{y}_i = y_i\, y_{m+i}\, y_{2m+i} \ldots \qquad \text{for } i = 1, \ldots, m.

Note: if the ciphertext is written in rows of mm letters, each 𝒚i\boldsymbol{y}_i is a column. The original ciphertext is recovered by reading the rows in order, taking one letter from each 𝒚i\boldsymbol{y}_i in turn.

If the keylength we used is correct, for each sub-ciphertext 𝒚i\boldsymbol{y}_i, since it is encrypted using the Shift Cipher, Ic(𝒚i)I_c(\boldsymbol{y}_i) will be close to the expected IOC of English language. This is because the individual terms are simply approximate permutations of the pi2p_i^2 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:

26⋅1262=126≈0.03826 \cdot \frac{1}{26^2} = \frac{1}{26} \approx 0.038

which is considerably different from 0.065.

We summarize this test below:

Technique (IOC test for keylength mm). Let mm be a positive integer. Divide the ciphertext into mm sub-ciphertexts:

𝒚i=yiym+iy2m+i…for i=1,…,m.\boldsymbol{y}_i = y_i\, y_{m+i}\, y_{2m+i} \ldots \qquad \text{for } i = 1, \ldots, m.

For each 𝒚i\boldsymbol{y}_i, if

Ic(𝒚i)≈0.065I_c(\boldsymbol{y}_i) \approx 0.065

then mm is very likely the correct value. Otherwise, mm is very likely incorrect.

If mm is the correct keylength, its multiples 2m,3m,…2m, 3m, \ldots will also pass this test, so we take the smallest mm 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 f0,…,f25f_0, \ldots, f_{25} denote the frequencies of A, …, Z in 𝒚i\boldsymbol{y}_i, respectively. If 𝒚i\boldsymbol{y}_i has ll letters, then the relative frequencies of A, …, Z are f0l,…,f25l\frac{f_0}{l}, \ldots, \frac{f_{25}}{l} respectively.

We define the quantity MgM_g that I will call the “displaced” IOC (DIOC); this is not a standard term, but it is a helpful name:

Mg=∑i=025pifi+glM_g = \sum_{i=0}^{25} p_i \frac{f_{i + g}}{l}

To determine the key value kik_i from MgM_g, we simply traverse through all values of g∈ℤ26g \in \mathbb{Z}_{26}. The largest value of MgM_g, which should be close to the expected value of 0.065, indicates the correct shift, and then ki=gk_i = g. This is because the relative frequency distribution of 𝒚i\boldsymbol{y}_i (addition reduced modulo 26)

fgl,…,f25+gl\frac{f_g}{l}, \ldots, \frac{f_{25+g}}{l}

now corresponds to the natural relative frequency distribution of the English language, p0,…,p25p_0, \ldots, p_{25}.

Thus, MgM_g is nearly the expected IOC of English language, which is 0.065.

On the other hand, if we use the wrong gg, then the resultant MgM_g will be considerably below 0.065. We provide an informal justification of this below, using a result known as the rearrangement inequality.

Proof. Let p1≥⋯≥pnp_1 \geq \cdots \geq p_n be a probability distribution. Let q1′,…,qn′q_1′, \ldots, q_n’ be a permutation of probability distribution q1,…,qnq_1, \ldots, q_n.

We claim that

∑k=1npkqk′\sum_{k=1}^{n} p_k q_k’

is maximized when q1′≥⋯≥qn′q_1′ \geq \cdots \geq q_n’.

Consider an arbitrary sum ∑k=1npkqk′\sum_{k=1}^{n} p_k q_k’. We claim we can do at least as well by swapping two qq values “out of order”.

More precisely, if qs′<qt′q_s’ < q_t’ for s<ts < t, then swap qs′q_s’ and qt′q_t’. Name the resultant distribution as

q1′′,…,qn′′q_1”, \ldots, q_n”

Then

∑k=1npkqk′′−∑k=1npkqk′=∑k=1npk(qk′′−qk′)=ps(qt′−qs′)+pt(qs′−qt′)=(ps−pt)(qt′−qs′)≥0\begin{aligned} \sum_{k=1}^{n} p_k q_k” – \sum_{k=1}^{n} p_k q_k’ &= \sum_{k=1}^{n} p_k (q_k” – q_k’) \\ &= p_s (q_t’ – q_s’) + p_t (q_s’ – q_t’) \\ &= (p_s – p_t) (q_t’ – q_s’) \\ &\geq 0 \end{aligned}

This means each swap can only ever increase the resultant sum. By performing this kind of swap we ultimately terminate at the final state

q1′≥⋯≥qn′q_1′ \geq \cdots \geq q_n’

Applying this in context: the correct shift lines up the frequencies of 𝒚i\boldsymbol{y}_i with p0,…,p25p_0, \ldots, p_{25} in the right order, giving the largest value, close to ∑pi2≈0.065\sum p_i^2 \approx 0.065. 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 (k1,…,km)(k_1, \ldots, k_m)). Suppose we know the keylength mm.

Divide the ciphertext 𝒚\boldsymbol{y} into mm sub-ciphertexts:

𝒚i=yiym+iy2m+i…for i=1,…,m.\boldsymbol{y}_i = y_i\, y_{m+i}\, y_{2m+i} \ldots \qquad \text{for } i = 1, \ldots, m.

For each sub-ciphertext 𝒚i\boldsymbol{y}_i of length ll, find the corresponding key value ki=gk_i = g by choosing the gg that makes the DIOC largest, close to the expected value 0.065:

Mg=∑i=025pifi+glM_g = \sum_{i=0}^{25} p_i \frac{f_{i + g}}{l}

We eventually obtain (k1,…,km)(k_1, \ldots, k_m) 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 mm and gg 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 mm 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 m=9m = 9 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 MgM_g 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

Trending