What is the Playfair cipher?

The Playfair cipher encodes a message two letters at a time, using a 5 × 5 square of letters built from a keyword. Because it swaps pairs instead of single letters, it hides the letter patterns that give simpler ciphers away. Charles Wheatstone invented it in 1854, and it's named after Lord Playfair, who promoted its use. The British Foreign Office first turned it down as too complicated, but British forces later used it in the Second Boer War and in World War I. Today it's popular with puzzle makers, escape rooms and teachers, since it needs only a pencil and paper.

How the square is built

Write the keyword into the square row by row, skipping any letter that's already there, then fill the rest with the other letters of the alphabet in order. J is left out and counts as I, so 25 letters fit. The keyword PICNIC gives this square:

P I C N A
B D E F G
H K L M O
Q R S T U
V W X Y Z

How the pairs are encoded

First, the message is split into pairs of letters. Balloon becomes BA LX LO ON: the two L's would share a pair, so an X goes between them. Then each pair is swapped by where its letters sit in the square:

Pair Where the letters sit What happens Result
BA Different rows and columns Each takes the letter in its own row at the other corner of their rectangle GP
LX Same column Each takes the letter below it, going from the bottom row back to the top SC
LO Same row Each takes the letter to its right, going from the end of the row back to the start MH
ON Different rows and columns The other corners of their rectangle, as for BA MA

So Balloon becomes GP SC MH MA.

Tool description

Choose a keyword and type it into the Keyword field, just below the Cipher menu, then enter the text to hide in the Plain text box. The Cipher text box fills with capital letters in pairs as you go. The Keyword starts empty, and without one the square is just the alphabet in order, without J. Insert sample shows See you at the park at noon encoded with the keyword PICNIC: XL FX UZ NU QM BC IU OI YF LZ MA. The Cipher menu holds the other classical ciphers, such as Atbash or the rail fence cipher. The swap button between the boxes flips the tool to decoding and carries the result into the input, and the Playfair Cipher Decoder starts out that way. All of it happens in your browser.

Examples

Plain text Keyword Cipher text
Lunch by the fountain GARDEN OS BF NC DY OH HM PH QD PI
Bring cookies and lemonade BAKERY AB LH YD SU QB MA PE LG MK HS IR FK
Cheese and apple pie ORCHARD HA LC LX GC IG OU SI DS LD
Picnic at 3 by the lake BASKET WP GI PI BC KV HO SN SE SZ
Jelly beans for everyone CANDY KB MW ON EF ND RG LT KA FQ HT AF

How the message is prepared

  • Only letters are encoded. Anything else, from spaces and digits to punctuation and line breaks, is dropped, so write numbers as words: the 3 of Picnic at 3 by the lake is lost.
  • J is treated as I, and accents are simply dropped, so é counts as a plain E. Letters from alphabets like Greek or Cyrillic don't make it into the cipher text.
  • Two same letters get an X between them only when they land in the same pair. In Cheese and apple pie, the two E's of cheese meet in one pair and get an X, while the two P's of apple fall into different pairs, AP PL, and don't.
  • If one letter is left over at the end, an X joins it, or a Q if that last letter is an X: Relax is split into RE LA XQ.

Keyword tips

  • Case doesn't matter and anything but letters is ignored, so picnic gives the same square as PICNIC. J counts as I, and a letter used twice goes into the square once. A keyword without letters, such as one of digits, gives the plain alphabet square.
  • A keyword with many different letters mixes the square better. After a short one, the last rows stay in alphabet order, like VWXYZ above.
  • The Playfair Cipher Decoder gives back the added X's and Q's, and I in place of J, so the reader fixes them by sense.