Playfair Cipher
Encrypts two letters at a time using a 5 by 5 square built from a keyword.
Key square
How it works
Playfair was the first practical cipher to encrypt pairs of letters. Charles Wheatstone invented it in 1854, and it carries the name of his friend Lord Playfair, who promoted it. The British Army used it in the Boer War and the First World War because it was quick to do in the field with nothing but paper.
Build the square. Write the keyword into a 5 by 5 grid, skipping repeated letters, then fill in the rest of the alphabet. I and J share a square.
Split the message into pairs. If a pair would be a double letter, put an X between them. If one letter is left over at the end, add an X.
Encrypt each pair by where its two letters sit in the square:
- Same row: take the letter to the right of each, wrapping round to the start.
- Same column: take the letter below each, wrapping round to the top.
- Otherwise: the letters mark two corners of a rectangle. Take the other two corners, each one staying in its own row.
To decrypt, do the reverse: left for rows, up for columns, and the same rectangle rule. Then remove the padding X letters by eye.
Because it works on pairs, there are about 600 possible units to count and not 26, which made it far tougher than simple substitution in its day.
Worked example
With the key "playfair example", HI forms a rectangle in the square and becomes BM.
Common questions
Why does my decoded message have extra X letters?
They were added during encryption to split double letters and to fill out the last pair. Read past them.
What happens to the letter J?
It is treated as I. When you decode, decide from context which one was meant.