This is an additional explanation to my post about ciphers.


The hardest part is to find the length of the key. To do that, we can use the Kasiski examination published by Friedrich Kasiski in 1863.


Kasiski examination.

To find the key length of a Vigenère key, we can use the Kasiski examination. To demonstrate how it works, I’ll be using the encrypted intro of my post about ciphers:

DEFLNFLZUROAJYEGHSIEKLBOPLLRMEPXPWNPRRBLOHZKNPWYRQZBAWAUKVXR
ZMPUWXCFGPTZQTFPOIJWAVAWVYUWNRAFPLJVKLBRVPGMKPTRXOHRFHAWBDDM
MYNILSEEWVOPFISHOXKGVATLPSHVOJVZTRLXGHSCMKASMPNTVZGDFXNSKPBQ
MLIOBFLBSXDJKFCMWGFSIBSTVARLLENFFEXBGSDPEWAWPREQWHZKTRQSXJSI
ATLZAXJYRKAMFHXYVWPVTDUSFYBVHUZOGSZXVCFDKIELVBLMOIXAZSMNELWR
FMKPHSSHDPEWHZRQGXVBVWGJKOGLTAOWJDFXVIYQWCFHXPSWLKEHLWKHKPXY
PUEAXSYTQVMGJWFLXGECTSMIKIPLHLGLFZTLQNVYZTAQBQXYPWKQHLMNWVOP
FBUHHITMFVPFAHTZBVPOUWCIAAVBLYOCXZTXVBUHZMFXVVBLROKTBXPWA
  1. Find repeating sequences in a text.

The longer the repeating sequence is, the better.

DEFLNFLZUROAJYEGHSIEKLBOPLLRMEPXPWNPRRBLOHZKNPWYRQZBAWAUKVXR
ZMPUWXCFGPTZQTFPOIJWAVAWVYUWNRAFPLJVKLBRVPGMKPTRXOHRFHAWBDDM
MYNILSEEWVOPFISHOXKGVATLPSHVOJVZTRLXGHSCMKASMPNTVZGDFXNSKPBQ
MLIOBFLBSXDJKFCMWGFSIBSTVARLLENFFEXBGSDPEWAWPREQWHZKTRQSXJSI
ATLZAXJYRKAMFHXYVWPVTDUSFYBVHUZOGSZXVCFDKIELVBLMOIXAZSMNELWR
FMKPHSSHDPEWHZRQGXVBVWGJKOGLTAOWJDFXVIYQWCFHXPSWLKEHLWKHKPXY
PUEAXSYTQVMGJWFLXGECTSMIKIPLHLGLFZTLQNVYZTAQBQXYPWKQHLMNWVOP
FBUHHITMFVPFAHTZBVPOUWCIAAVBLYOCXZTXVBUHZMFXVVBLROKTBXPWA
  1. Calculate the distance between them.
Sequence Distance
WVOPF 288
DPEW 90
  1. Find all possible key lengths.

The biggest (distance itself) and the smallest ($1$) are highly unlikely to be the actual length. Therefore, we’re left with these dividers:

\[\begin{aligned} 288 &: \; 144,\ 96,\ 72,\ 48,\ 36,\ 32,\ 24,\ 18,\ 16,\ 12,\ 9,\ 8,\ 6,\ 4,\ 3,\ 2 \\ 90 &: \; 45,\ 30,\ 18,\ 15,\ 10,\ 9,\ 6,\ 5,\ 3,\ 2 \end{aligned}\]
  1. Determine the key candidate.

To do that, let’s leave only common dividers from the previous step:

\[18,\ 9,\ 6,\ 3,\ 2\]

The key length is most likely $18$, but if it’s not, we can always repeat the next steps with other dividers.


Caesar groups analysis.

The key length is $18$, so let’s make $18$ groups (every 18th letter, starting from the 1st, 2nd, … 18th):

DIRAQAXEPAMFGQFZOSKFPEQMAOR    Most common letter is A
EERUTFOESSLSSSHOIHOHUCNNHCO    Most common letter is S
FKBKFPHWHMIIDXXGXDGXETVWTXK    Most common letter is X
LLLVPLRVVPOBPJYSAPLPASYVZZT    Most common letter is L
NBOXOJFOONBSESVZZETSXMZOBTB    Most common letter is O
FOHRIVHPJTFTWIWXSWAWSITPVXX    Most common letter is W
LPZZJKAFVVLVAAPVMHOLYKAFPVP    Most common letter is V
ZLKMWLWIZZBAWTVCNZWKTIQBOBW    Most common letter is W
ULNPABBSTGSRPLTFERJEQPBUUUA    Most common letter is U
RRPUVRDHRDXLRZDDLQDHVLQHWH    Most common letter is R
OMWWAVDOLFDLEAUKWGFLMHXHCZ    Most common letter is W
AEYXWPMXXXJEQXSIRXXWGLYIIM    Most common letter is X
JPRCVGMKGNKNWJFEFVVKJGPTAF    Most common letter is J
YXQFYMYGHSFFHYYLMBIHWLWMAX    Most common letter is Y
EPZGUKNVSKCFZRBVKVYKFFKFVV    Most common letter is K
GWBPWPIACPMEKKVBPWQPLZQVBV    Most common letter is P
HNATNTLTMBWXTAHLHGWXXTHPLB    Most common letter is T
SPWZRRSLKQGBRMUMSJCYGLLFYL    Most common letter is L

But how do we determine the key? We could have just assumed that the most common letter in each group must be E, just like it is in the English language, but if we did that, we would have gotten:

\[\begin{aligned} \mathrm{A}=\mathrm{E} &\Rightarrow \text{KEY} = 0-4 = -4 \equiv 22 = \mathrm{W} \\ \mathrm{S}=\mathrm{E} &\Rightarrow \text{KEY} = 18-4 = 14 = \mathrm{O} \\ \mathrm{X}=\mathrm{E} &\Rightarrow \text{KEY} = 23-4 = 19 = \mathrm{T} \\ \mathrm{L}=\mathrm{E} &\Rightarrow \text{KEY} = 11-4 = 7 = \mathrm{H} \\ \mathrm{O}=\mathrm{E} &\Rightarrow \text{KEY} = 14-4 = 10 = \mathrm{K} \\ \mathrm{W}=\mathrm{E} &\Rightarrow \text{KEY} = 22-4 = 18 = \mathrm{S} \\ \mathrm{V}=\mathrm{E} &\Rightarrow \text{KEY} = 21-4 = 17 = \mathrm{R} \\ \mathrm{W}=\mathrm{E} &\Rightarrow \text{KEY} = 22-4 = 18 = \mathrm{S} \\ \mathrm{U}=\mathrm{E} &\Rightarrow \text{KEY} = 20-4 = 16 = \mathrm{Q} \\ \mathrm{R}=\mathrm{E} &\Rightarrow \text{KEY} = 17-4 = 13 = \mathrm{N} \\ \mathrm{W}=\mathrm{E} &\Rightarrow \text{KEY} = 22-4 = 18 = \mathrm{S} \\ \mathrm{X}=\mathrm{E} &\Rightarrow \text{KEY} = 23-4 = 19 = \mathrm{T} \\ \mathrm{J}=\mathrm{E} &\Rightarrow \text{KEY} = 9-4 = 5 = \mathrm{F} \\ \mathrm{Y}=\mathrm{E} &\Rightarrow \text{KEY} = 24-4 = 20 = \mathrm{U} \\ \mathrm{V}=\mathrm{E} &\Rightarrow \text{KEY} = 21-4 = 17 = \mathrm{R} \\ \mathrm{P}=\mathrm{E} &\Rightarrow \text{KEY} = 15-4 = 11 = \mathrm{L} \\ \mathrm{T}=\mathrm{E} &\Rightarrow \text{KEY} = 19-4 = 15 = \mathrm{P} \\ \mathrm{L}=\mathrm{E} &\Rightarrow \text{KEY} = 11-4 = 7 = \mathrm{H} \end{aligned}\]

And as a person who encrypted the original text, I can assure you the key is either a word or a phrase, not just gibberish.

This would have worked, if our text sample were longer. But since it’s not, we should take a look not at just one most common letter but preferably 3-4 of them. From here we have two options.


Option 1.

We can try assigning the second, third, and so on most common letters in the group to E, which would work faster for the first group, for example:

Most frequent letters: $\mathrm{A},\ \mathrm{F},\ \mathrm{Q}$

For $\mathrm{A}=\mathbf{E}$:

  • $\mathrm{F} = \mathrm{J}$
  • $\mathrm{Q} = \mathrm{U}$

For $\mathrm{F}=\mathbf{E}$:

  • $\mathrm{A} = \mathrm{Z}$
  • $\mathrm{Q} = \mathrm{P}$

For $\mathrm{Q}=\mathbf{E}$:

  • $\mathrm{A} = \mathbf{O}$
  • $\mathrm{F} = \mathbf{T}$

In bold I marked the first 9 most frequent letters of the English alphabet (frequency is higher than 50%). We can see that $\mathrm{Q}=\mathrm{E}$ is most probably the right combination, so:

\[\text{KEY} = 16 - 4 = 12 = \mathrm{M}\]

Option 2.

Or we can try assigning the most common letter of the group to the second, third, and so on most common letters in the English alphabet. Let’s try it on the second group:

Most frequent letters: $\mathrm{S},\ \mathrm{H},\ \mathrm{O}$

For $\mathrm{S}=\mathrm{E}$:

  • $\mathrm{H} = \mathrm{T}$
  • $\mathrm{O} = \mathrm{A}$

For $\mathrm{S}=\mathrm{T}$:

  • $\mathrm{H} = \mathbf{I}$
  • $\mathrm{O} = \mathrm{P}$

For $\mathrm{S}=\mathrm{A}$:

  • $\mathrm{H} = \mathrm{P}$
  • $\mathrm{O} = \mathrm{W}$

$\dots$

For $\mathrm{S}=\mathrm{S}$:

  • $\mathrm{H} = \mathbf{H}$
  • $\mathrm{O} = \mathbf{O}$
In our example most of the groups are solved faster with the first option.


After we determined the key for each row, we can build the whole key, which, for our example, was:

MATHBEHINDSECURITY

Now we just use this key to decrypt the ciphertext using the Vigenère decoder and get the original text:

REMEMBERHOWWHENYOUWEREAKIDYOUANDYOURFRIENDSCAMEUPWITHYOUROWN
SECRETALPHABETMINECONSISTEDOUTOFWEIRDDOODLESTHATLOOKEDTOOALI
KEWASUSEDONLYAFEWTIMESANDSOONFORGOTTENBUTMOSTIMPORTANTLYTHIS
ALPHABETFULFILLEDITSPURPOSEITALLOWEDUSKIDSTOCOMMUNICATESECRE
TLYWITHEACHOTHERUSINGACODEKNOWNONLYTOUSASECRETSOCIETYOFFRIEN
DSTHOUGHKIDSARENOTTHEONLYONESWHOWANTTOHIDETHEIRSECRETSINTHEA
DULTWORLDSUCHCODEISCALLEDACIPHERORANENCRYPTIONFUNCTIONANDONL
YTHEPERSONWHOHASARIGHTKEYGETSACCESSTOTHEHIDDENINFORMATION