Playfair Cipher

Digraph substitution cipher with a 5×5 keyword square — encrypt and decrypt paired letters.

Key Square
Plaintext
Ciphertext
Enter plaintext and click Encrypt.

How to Use the Playfair Cipher Tool

  1. Enter a keyword — type any keyword (e.g., MONARCHY) and click Apply. The 5×5 key square updates automatically.
  2. Choose Encrypt or Decrypt — select the direction with the option chips.
  3. Enter your text — type or paste plaintext. Letters only; spaces and punctuation are ignored.
  4. View the output — the encrypted (or decrypted) text appears on the right.
  5. Key Square tab — see the current 5×5 grid used for encryption.

About the Playfair Cipher

The Playfair cipher was invented by Sir Charles Wheatstone in 1854 but popularized by Lord Lyon Playfair, who advocated its use to the British Foreign Office, giving the cipher its name. It was the first practical digraph substitution cipher in widespread use. Unlike simple monoalphabetic ciphers, Playfair encrypts pairs of letters, making simple frequency analysis much harder to apply.

Building the Key Square

The 5×5 key square is built from a keyword: write the unique letters of the keyword left-to-right, top-to-bottom in the grid, then fill in the remaining unused letters of the alphabet in alphabetical order. I and J share one cell (typically both mapped to I). For the keyword MONARCHY, the grid starts: M, O, N, A, R, C, H, Y then continues B, D, E, F, G, I/J, K, L, P, Q, S, T, U, V, W, X, Z.

The Three Encryption Rules

The plaintext is divided into digraphs (pairs). If a pair has the same letter, an X is inserted between them. If the message has an odd length, X is appended. Then for each digraph:

  • Same row: replace each letter with the letter immediately to its right in the row (wrapping around).
  • Same column: replace each letter with the letter immediately below it in the column (wrapping around).
  • Rectangle (different row and column): each letter is replaced by the letter in the same row but in the column of the other letter of the pair.

Decryption uses the same rules in reverse: shift left instead of right, shift up instead of down, and the rectangle rule is the same (it is symmetric).

Historical Use

The Playfair cipher was used extensively by the British military during World War I and was also used by the Australians and British in World War II. It was considered secure enough for tactical communications but not for high-level strategic messages, which used more complex cipher systems. The cipher gained cultural fame when John F. Kennedy, stranded after the sinking of PT-109, used a Playfair cipher to encode a rescue message scratched onto a coconut shell.

Cryptanalysis

With sufficient ciphertext (around 200 characters), Playfair can be broken using frequency analysis of digraphs rather than individual letters. The cipher produces 676 possible digraphs (26×26), but since I and J are merged and certain rules apply, patterns emerge. Importantly, Playfair never maps a digraph to the same reversed digraph: if AB encrypts to XY, then XY encrypts to AB, giving a known relationship that aids breaking. Modern computer-based attacks can break Playfair quickly using simulated annealing or hill-climbing algorithms.

Frequently Asked Questions

The Playfair cipher is a digraph substitution cipher invented by Charles Wheatstone in 1854. It encrypts pairs of letters using a 5x5 keyword grid. It was used by the British in WWI and WWII and is famous partly because JFK used it to encode a rescue message after the sinking of PT-109.
If a digraph pair has identical letters (like LL in HELLO), an X is inserted between them (HE LX LO). This is why decrypted text may contain extra X letters that should be removed by the reader using context.
1. Same row: shift right (wrapping). 2. Same column: shift down (wrapping). 3. Rectangle: swap to the opposite corner of the rectangle formed by the two letters in the grid.
The 5x5 grid has 25 cells but the English alphabet has 26 letters. I and J share one cell since they're phonetically similar. Context usually makes the correct letter clear during decryption.
Playfair resists simple letter frequency analysis because it encrypts pairs. However, with about 200+ characters of ciphertext, modern digraph frequency analysis or computer search can break it. It's a historical cipher, not suitable for modern security needs.