Skip to content

↑↓ navigate ↵ open Esc close Search runs locally

Playfair Cipher Encoder & Decoder

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.

  • Runs in your browser
  • Nothing is uploaded
  • Free, no sign-up
0 chars
Playfair
Runs in your browser. Nothing is uploaded. Tip: J is merged with I, and X separates double letters

How to use the Playfair cipher tool#

  1. Choose Encrypt or Decrypt and type a Key. The Key square under the tool shows the 5×5 grid it creates.
  2. Type or paste your text. Only letters are used, and J is treated as I.
  3. When encrypting, double letters in a pair are split with the Filler letter (X by default), and an odd last letter gets one too.
  4. Tick Group in pairs to show the output as two-letter groups, then copy it.

Examples#

The classic Wikipedia example

Input
Hide the gold in the tree stump
Output
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.

Decrypt the same message

Input
BMODZBXDNABEKUDMUIXMMOUVIF
Output
HIDETHEGOLDINTHETREXESTUMP

The filler X stays in the decrypted text ("TREXES"). The reader removes it by context.

Output grouped in pairs

Input
Hello
Output
DM YR AN

"Hello" becomes HE LX LO before encryption, because the double L must be split.

What is the Playfair cipher?#

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.

Building the key square#

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:

plaintext
P L A Y FI R E X MB C D G HK N O Q ST U V W Z

The encryption rules#

First 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 squareEncryptDecrypt
Same rowTake the letter to the right of each (wrapping around)Take the letter to the left
Same columnTake the letter below each (wrapping around)Take the letter above
Different row and columnSwap the columns: each letter is replaced by the one in its own row and the other letter's columnThe same

For example, HI sits in a rectangle (H at row 3, column 5; I at row 2, column 1), so it becomes BM.

Strengths and weaknesses#

  • There are about 25! possible key squares, far too many to try them all.
  • But digraph frequencies still leak through: common English pairs such as TH and HE produce common ciphertext pairs, and a pair and its reverse (AB and BA) always encrypt to reversed pairs.
  • No pair ever encrypts to a double letter, and a letter never encrypts to itself, which helps codebreakers.

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.

Playfair in code#

javascript
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];}

Common errors and fixes#

  • 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.

Frequently asked questions#

Why are I and J combined in Playfair?

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.

What happens to double letters?

A pair cannot contain the same letter twice, so a filler (X by default) is inserted between them. "Hello" becomes HE LX LO.

Who invented the Playfair cipher?

Charles Wheatstone in 1854. It is named after Lord Playfair, who championed its use by the British government.

Can Playfair be broken?

Yes, with enough ciphertext. Digraph frequency analysis and computer search methods recover the key square.

Is my text uploaded?

No. Everything runs in your browser.

Your next tool is one search away.

Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.

Browse all tools

Tip: press Ctrl D to bookmark dev247.