The classic Wikipedia example
Hide the gold in the tree stump
BMODZBXDNABEKUDMUIXMMOUVIF
The pairs are HI DE TH EG OL DI NT HE TR EX ES TU MP: the double E in "tree" is split with an X.
Encrypt and decrypt with the Playfair cipher. Type a keyword and see the 5×5 key square build live; letters are encrypted in pairs using the classic row, column and rectangle rules.
Key square
Hide the gold in the tree stump
BMODZBXDNABEKUDMUIXMMOUVIF
The pairs are HI DE TH EG OL DI NT HE TR EX ES TU MP: the double E in "tree" is split with an X.
BMODZBXDNABEKUDMUIXMMOUVIF
HIDETHEGOLDINTHETREXESTUMP
The filler X stays in the decrypted text ("TREXES"). The reader removes it by context.
Hello
DM YR AN
"Hello" becomes HE LX LO before encryption, because the double L must be split.
The Playfair cipher encrypts pairs of letters (digraphs) using a 5×5 square of letters built from a keyword. Because it works on pairs instead of single letters, the same letter is encrypted differently depending on its partner, which makes simple frequency analysis much harder than for a Caesar or affine cipher.
It was invented by Charles Wheatstone in 1854 and named after Lord Playfair, who promoted it. It was used as a field cipher by the British in the Boer War and the First World War, because it was quick to use by hand.
Write the letters of the keyword into a 5×5 grid, skipping any letter already used, then fill the rest of the grid with the remaining letters of the alphabet in order. There are only 25 cells, so I and J share one. With the key PLAYFAIR EXAMPLE:
P L A Y FI R E X MB C D G HK N O Q ST U V W ZFirst prepare the message: remove everything but letters, change J to I, and split it into pairs. If a pair would contain the same letter twice, insert an X between them; if one letter is left at the end, add an X. Then encrypt each pair:
| Position in the square | Encrypt | Decrypt |
|---|---|---|
| Same row | Take the letter to the right of each (wrapping around) | Take the letter to the left |
| Same column | Take the letter below each (wrapping around) | Take the letter above |
| Different row and column | Swap the columns: each letter is replaced by the one in its own row and the other letter's column | The same |
For example, HI sits in a rectangle (H at row 3, column 5; I at row 2, column 1), so it becomes BM.
TH and HE produce common ciphertext pairs, and a pair and its reverse (AB and BA) always encrypt to reversed pairs.With enough ciphertext, Playfair is broken with digraph frequency analysis or computer methods such as hill climbing.
Playfair is not secure today. It was a good field cipher in 1900, but a computer can break it from a few hundred letters of ciphertext.
function square(key) { const seen = new Set(); return [...(key.toUpperCase().replace(/J/g, 'I') + 'ABCDEFGHIKLMNOPQRSTUVWXYZ')] .filter((c) => c >= 'A' && c <= 'Z' && !seen.has(c) && seen.add(c));} function encryptPair(sq, a, b) { const [ra, ca] = [Math.floor(sq.indexOf(a) / 5), sq.indexOf(a) % 5]; const [rb, cb] = [Math.floor(sq.indexOf(b) / 5), sq.indexOf(b) % 5]; if (ra === rb) return sq[ra * 5 + (ca + 1) % 5] + sq[rb * 5 + (cb + 1) % 5]; if (ca === cb) return sq[((ra + 1) % 5) * 5 + ca] + sq[((rb + 1) % 5) * 5 + cb]; return sq[ra * 5 + cb] + sq[rb * 5 + ca];}The decrypted message has extra X letters
That is normal. X was inserted between double letters and at the end when encrypting, and Playfair cannot tell which X letters are real. Remove them by reading the text.
My result differs from another Playfair tool
Check three conventions: which letters share a cell (I/J here; some tools drop Q instead), which filler letter is used, and whether the key square is filled row by row. This tool uses the most common convention.
Decryption gives nonsense
The keyword is wrong or the ciphertext was split incorrectly. Playfair ciphertext always has an even number of letters and never contains J.
The square has 25 cells and the alphabet has 26 letters, so one letter must go. Merging J into I is the usual choice because J is rare and easy to guess from context.
A pair cannot contain the same letter twice, so a filler (X by default) is inserted between them. "Hello" becomes HE LX LO.
Charles Wheatstone in 1854. It is named after Lord Playfair, who championed its use by the British government.
Yes, with enough ciphertext. Digraph frequency analysis and computer search methods recover the key square.
No. Everything runs in your browser.
Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.
Tip: press Ctrl D to bookmark dev247.