Playfair Cipher Encrypt & Decrypt

Digraph substitution using a 5×5 keyword-keyed square.

Free Playfair cipher tool that encrypts and decrypts letter pairs on a 5×5 keyword square. Implements the real same-row, same-column and rectangle rules with I/J merging, in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

How does the Playfair cipher work?

A 5×5 square is built from a keyword plus the rest of the alphabet, with I and J sharing a cell. The message is split into letter pairs and each pair is replaced according to its position in the square.

The Playfair cipher was the first practical digraph substitution cipher, encrypting pairs of letters at a time instead of single letters. Invented in 1854 and named after Lord Playfair, it was used for tactical military messages because it could be performed by hand yet resisted casual frequency analysis. This tool builds the keyword square and applies the real rules in your browser.

How it works

First build a 5×5 square: write the keyword (dropping repeats) into the grid, then fill the remaining cells with the rest of the alphabet. Because the grid has 25 cells, I and J share one cell.

Split the plaintext into pairs. If a pair would contain two identical letters, insert an X between them; if the message has an odd length, pad the final letter with X. Then apply three rules to each pair:

  • Same row: replace each letter with the one immediately to its right, wrapping around.
  • Same column: replace each letter with the one immediately below it, wrapping around.
  • Rectangle: replace each letter with the one in its own row at the other letter’s column.

Decryption uses the same rules but shifts left and up instead of right and down.

Building the square step by step

Take the keyword MONARCHY. Write it out, skipping any repeated letters: M O N A R C H Y. Then continue with the unused letters of the alphabet in order, skipping I (merged with J): B D E F G I/J K L P Q S T U V W X Z. The resulting square is:

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

Every keyword produces a unique square, and the entire cipher’s security rests on keeping the keyword secret.

Worked example: encrypting BALLOON

The word BALLOON contains repeated letters that need handling before encryption. Steps:

  1. Pair the letters: BA LL OO N.
  2. The pair LL has two identical letters — insert X: BA LX LO ON.
  3. No trailing single letter, so no final-X pad needed.
  4. Now encrypt each digraph against the MONARCHY square above.

For the BA pair: B is at row 1 col 3; A is at row 0 col 3 — same column, so each shifts one row down: B → I, A → B. Output: IB. Continuing through each pair produces the ciphertext.

Why Playfair resists simple frequency analysis

Standard Caesar and simple substitution ciphers preserve letter frequency, so common letters (E, T, A in English) show up frequently in the ciphertext and reveal themselves. Playfair encrypts pairs, so the 26 single-letter frequencies are replaced by 676 possible digraph frequencies, which is far harder to exploit — especially in short messages. However, patterns in common digraphs (TH, HE, IN) can still leak information to a determined analyst with enough ciphertext, and the cipher was broken by British intelligence in the early twentieth century.

CipherUnit encryptedKey typeResistance to frequency analysis
CaesarSingle letterShiftVery low
VigenèreSingle letterKeyword (repeating)Low to moderate
PlayfairLetter pair5×5 keyword squareModerate
Four-squareLetter pairTwo 5×5 squaresModerate
EnigmaSingle letterRotor settingsHigh (for its era)

Notes on decryption and padding

Decrypted output will include any padding X letters that were inserted before encryption. These are usually obvious from context — BALXLOON decrypts back to BALLOON. Since I and J share a cell, context tells you which was meant: BALLON containing an I/J cell should be read as the letter that makes a real word.