Playfair cipher: square and converter
The Playfair cipher replaces pairs of letters with other pairs according to a 5 × 5 square built from a keyword. We cut the message into pairs, find each pair in the square, and depending on how the two letters lie in it, turn it into a different pair. The alphabet in a 5 × 5 square is also used by the Polybius square, but you don’t need to know it, everything you need is here.
How to build the square
The sender and the receiver first agree on a keyword. Let’s take HÁJENKA (Czech for “gamekeeper’s lodge”) and leave out the accent over the Á. We write the letters of the key into the square row by row, each only at its first occurrence, and continue with the rest of the alphabet. There are only 25 cells and 26 letters, so I and J share one place and J becomes I:
| H | A | I | E | N |
| K | B | C | D | F |
| G | L | M | O | P |
| Q | R | S | T | U |
| V | W | X | Y | Z |
So HAJENKA was left as HAIENK: J is now I, and the second A was already in the square, so we skip it.
Splitting the message into pairs
We will encrypt “ranní sraz u lesa” (Czech for “morning meeting by the forest”). Without spaces and accents that is RANNISRAZULESA. Write digits out in words, and treat CH as C and H. Now we split the letters into pairs. The second pair would come out as NN straight away, but a pair of two identical letters is not encrypted, because it would give away the doubled letter. So we insert the letter X between the two Ns and carry on: RA NX NI SR AZ UL ES A. At the end a lone A is left, so we add an X to it too:
RA NX NI SR AZ UL ES AX
Q is used instead of X as the filler only when the letter to be filled in is itself X: a doubled XX or a single X at the end.
The three rules of the Playfair cipher
The two letters of a pair lie in the square either in the same row, or in the same column, or in neither. Each case has its own rule:
- Same row: we replace each letter with the one to the right of it. After the end of the row we return to its start.
- Same column: we replace each letter with the one below it. After the bottom row we continue with the top one.
- Elsewhere: each letter stays in its own row and moves to the column of the other letter. For NX, for example: N from the top row moves to the column of X and becomes I, X from the bottom row moves to the column of N and becomes Z. So NX is IZ. The two letters of the cipher are the other two corners of the rectangle.
The letters of the message are red, the letters of the cipher blue. And now the whole message:
- RA is in the second column. Below R lies W, below A lies B. RA is WB.
- NX is a rectangle, which we have already solved: IZ.
- NI is in the top row. To the right of I is E, but after N the row ends, so we jump to its start at H. NI is HE.
- SR lies in the fourth row: S becomes T, R becomes S, giving TS.
- AZ, UL, ES and AX are again rectangles: NW, RP, IT and IW.
So the cipher reads:
WB IZ HE TS NW RP IT IW
With a rectangle, watch one thing: the first letter of the cipher is always in the row of the first letter of the message.
How to decrypt a Playfair cipher
The receiver builds the same square and works backwards: in the same row they take the letter to the left, in the same column the letter above. For a rectangle nothing changes: the corners in the same row are simply swapped again. From WB IZ HE TS NW RP IT IW they get RA NX NI SR AZ UL ES AX. They recognise an inserted X by the sense: it stands between two identical letters or at the very end. A real X in a word is rare in Czech. After crossing them out they read “ranni sraz u lesa”.
Playfair cipher converter
Type in a message and a keyword. The converter drops spaces, punctuation and Czech accents, writes J as I and outputs the result in lower case in pairs. When decrypting it does not remove the inserted Xs, that is up to you.
Why it is stronger than Caesar
The Caesar cipher and the substitution cipher always change one letter into the same other letter. That is why frequency analysis gives them away easily: the most common letter of the cipher most likely stands for E or A.
Playfair encrypts pairs, and the same letter comes out differently every time depending on who it is paired with. In our example A changed once into B, once into N and once into I. Counting single letters is not enough here. The codebreaker has to track pairs, and there are 25 · 24 = 600 of them, because the second letter must not be the same as the first. Finding your way among 600 pairs is much harder than among 26 letters.
Why it can still be broken
But pairs of letters also have their frequencies in a language. In Czech, for example, ST, PR or NE recur often, and in a longer cipher their encrypted forms will appear just as often. Playfair also leaves traces: the cipher never contains a pair of two identical letters, and the pairs AB and BA are encrypted in reverse, so when you find WB in the message you know that BW means AR. With a long text and a little patience the square can be put back together. For the few lines that a troop gets at camp, though, it is a real chore.
Playfair as a cipher for camp
Children aged about 12 and over can manage encrypting with the square. Preparation takes a few minutes:
- Choose a keyword related to the camp, such as HÁJENKA, KOTLÍK (Czech for “billycan”) or OHNIŠTĚ (Czech for “fire pit”). The more different letters, the better the square is shuffled.
- Give younger groups a ready-made square: just print the table from this article. Older ones build it themselves when they are given only the key.
- Keep the message short, 10 to 20 letters is enough for children. Most mistakes happen when splitting into pairs, especially with doubled letters.
- Put the finished cipher into the converter above, switch to Decrypt and check that your message comes out.
The rules can be summed up for children in three words: right, down, corners. To help codebreakers without the key, hint that the key is, say, the name of one of the leaders.
Where it comes from
The cipher was invented by the English physicist Charles Wheatstone; the oldest surviving description bears his signature and the date 26 March 1854. It got its name, however, from his friend Lord Playfair, who promoted it to the British government. A Foreign Office official is said to have objected that it could never be taught to attachés. The British Army then used it in the Boer War and in the First World War. In the Second World War it was used by British and Australian coastwatchers in the Pacific, including in August 1943 during the rescue of the crew of the torpedo boat PT-109, commanded by the future president John F. Kennedy.