It is well known that certain letters, such as 'E' and 'T', appear more frequently in English than other letters (see here for more details on individual letter frequencies), and, unsurprisingly, it turns out that certain bigrams (pairs of letters, such as 'AC') appear more frequently than other bigrams.
The spreadsheet excerpt below is based on the complete works of Shakespeare and also some of the works by Charles Dickens. In total, it counted over 5,000,000 bigrams to generate the statistics that you can see. The 45 most frequent bigrams are displayed and if you click on the download icon, you can download the whole spreadsheet which contains the frequencies for all 676 possible bigrams.
This message has been created using a simple transposition cipher and the key of 3-1-4-2:
Although the key is 3-1-4-2, this is for converting the plaintext into the ciphertext; the key to go in the other direction is 2-4-1-3, and this is the key that we shall be trying to find below.
First, we shall assume that we have tested without success all keys that are 2 or 3 digits long, and we have now got to keys that are 4 digits long. One option is to brute force all of them, but we are going to use bigram frequencies instead, as this method will also enable us to find longer keys much more quickly.
To start this process, we will arrange the ciphertext into rows, with each row being the size of the key length being considered (i.e. 4 in this case):
We can reconstruct the ciphertext (not plaintext) from this table by just reading from left-to-right and then down the rows.
We can reconstuct the plaintext by first putting the columns in the correct order and then reading from left-to-right and then down the rows.
But what is the correct order of the columns? Let's start with a simpler question: what column should come after column 1? Is it column 2, 3 or 4?
We can try out each pairing of 1-2, 1-3 and 1-4 and then look down the pair of columns to see the bigrams that have been generated. We can then consult our data about bigram frequencies to see which of the pairings generates the bigrams that occur most frequently in English. In the table below, I have tried out the three pairings of 1-1, then 1-2 and finally 1-3, and then I have added the bigram score from my Shakespeare / Dickens data mentioned above and I have placed the total of all these scores for that column in the yellow cell near the top:
The three bigram scores are as follows:
We can see that we get a much higher bigram score if column 1 is followed by column 3. We do not know the final key, but we can be pretty confident that, in the final key, 1 is followed by 3 as this has generated the bigrams most resembling normal English text.
If we repeat this process for column 2, which can be followed by column 1, 3 or 4, we get the following scores:
We can see that we get a much higher bigram score if column 2 is followed by column 4. Let's repeat this process for the last two columns 3 and 4:
There is no clear answer from column 3, but there is for column 4 - it should be followed by column 1. So we have established the following:
If we put these together, we can see that we get the key: 2,4,1,3, which is the key mentioned near the top of this page.
Why was there no clear answer for column 3? This is because column 3 is at the end of the key, so it is not followed by a letter from any of the other columns on that row - so none of the pairings generated a typical set of English bigrams. Instead, it is followed by a letter from the next row.
I have used a key length of just 4 on this page. This has made it easier to illustrate how it is used but it has hidden its power if we compare the bigram frequency method with a brute force method. For a key length of 4:
We are not looking at exactly the same thing with each method, but this will not matter if there is a vast difference between the number of possibilities that we need to look at in each case. Let's look at the equivalent figures for a key length of 12:
When the key length is 12, the brute force method requires us to look at nearly 4,000,000 more possibilities than the bigram frequency method!