✖

Substitution cipher: keyword and converter

A substitution cipher replaces every letter of the message with another one according to a cipher alphabet agreed in advance. It is also called a monoalphabetic cipher, because a single alphabet encrypts the whole text. The easiest way to make one is from a keyword known only to the sender and the receiver. Let’s choose the key TÁBOR (Czech for “camp”). We write it at the start of the cipher alphabet (without the accent, so TABOR) and the rest of the alphabet after it:

The plain alphabet A to Z split into two rows of thirteen letters, with its replacement under each letter: T A B O R in red, then C D E F G H I J K L M N P Q S, and finally U V W X Y Z in green, because they stayed in their places The plain alphabet A to Z split into two rows of thirteen letters, with its replacement under each letter: T A B O R in red, then C D E F G H I J K L M N P Q S, and finally U V W X Y Z in green, because they stayed in their places

On top is the original letter, below it its replacement. A is written as T, B as A, H as E. We encrypt “sraz u ohně” (Czech for “meet at the campfire”, leaving out the accent):

  • S changes to Q, R to P, A to T, and Z stays Z, so “sraz” becomes QPTZ,
  • U stays U,
  • O changes to L, H to E, N to K and E to R, so “ohne” becomes LEKR.

The message reads “qptz u lekr”. Whoever receives it and knows the key writes out the alphabet in the same way and reads it backwards: they find the letter in the bottom row, and the original stands above it.

Substitution cipher converter

Enter a text and a keyword. The converter drops Czech accents, writes in lower case and leaves spaces and punctuation where they are. From the key it takes only the letters, each once. To decrypt, enter the same keyword the sender used.

How to make a cipher alphabet from a key

  1. Write the keyword without accents.
  2. Keep each letter only at its first occurrence and cross out the repeats.
  3. After the key, write the rest of the alphabet in the usual order, leaving out the letters already in the key.
  4. Above the cipher alphabet, write the normal alphabet A to Z.

Let’s try the key KOSTKA (Czech for “cube” or “die”). The second K is extra, so KOSTA is left. After it we write the rest of the alphabet without K, O, S, T and A: B, C, D, E, F, G, H, I, J, L, M, N, P, Q, R, U, V, W, X, Y, Z. A is then written as K, B as O, C as S. The key can also be a longer password or a whole sentence, for example the motto of a scout troop. The more different letters it contains, the more thoroughly it shuffles the alphabet.

Watch out for the end of the alphabet. The key takes the first places and the rest simply shifts after it. The letters after the last letter of the key (for TÁBOR that is T) therefore end up back in their own places: U to Z do not change, which is why “u” and “z” stayed the same in the text. This is the weakness of a keyword and it gives a lot away to the codebreaker. It helps to use a key with a letter from the end of the alphabet, such as ZUBR (Czech for “bison”), or an alphabet shuffled completely at random.

How many keys a substitution cipher has

When we don’t make the cipher alphabet from a keyword but shuffle it freely, we simply get some order of the 26 letters. The key is then the whole alphabet. In the first place we can put any of the 26 letters, in the second any of the remaining 25, in the third any of 24, and so on. The number of different orders is therefore

26!=26⋅25⋅24⋯2⋅1,

which is read “26 factorial” and means multiplying all the numbers from 26 down to one (more in the article on the factorial). It comes to exactly 403,291,461,126,605,635,584,000,000, which is roughly 4 × 1026 keys.

For comparison, the Caesar cipher only shifts the alphabet, so it has 25 keys and all of them can be tried in a few minutes. If we tried a billion keys per second for a substitution cipher, it would take over ten billion years. Brute force is out. But there are far fewer alphabets made from keywords, and a codebreaker can go through them using a dictionary.

How to break it anyway

It sounds like an unbreakable cipher, but it is not. A substitution does not change how often each letter appears in the message: if E appears ten times in the message, its replacement will appear ten times in the cipher. In Czech the most common letter is E, followed by A and O (the table is in the article on frequency analysis). With the key TÁBOR, E is written as R, A as T and O as L, so in a long encrypted message the most common letters will be R, T and L.

The codebreaker therefore proceeds like this:

  1. Count how many times each letter appears.
  2. Tentatively replace the most common letters with E, A and O.
  3. Then look at the short words. Czech one-letter words tend to be a, i, o, u, v, k, s or z, two-letter words things like je, se, na, to. With the key TÁBOR, “je” is GR and “se” is QR, so anyone who already knows that R is E quickly spots G and Q.
  4. Fill in further letters until whole words emerge from the text.

The longer the message, the better this works. With a few words the frequencies don’t show yet, but with a paragraph they do. The codebreaker doesn’t try keys, they read the key out of the message letter by letter.

The substitution cipher and other simple ciphers

Most simple ciphers are really a substitution with a special cipher alphabet:

All of them therefore fall the same way, to frequency analysis. A different kind is the rail fence cipher: it doesn’t change the letters, it only rearranges them. It is called a transposition cipher and has the same letter frequencies as the language itself.

From history

Frequency analysis was already described in the 9th century by the Arab scholar al-Kindi in Baghdad. Mary, Queen of Scots, was undone by it: in 1586 she wrote from prison to the conspirators against Queen Elizabeth of England in a substitution cipher supplemented with symbols, the codebreaker Thomas Phelippes read the letters, and Mary was executed on 8 February 1587.