Chris Spackman's NeoCities Page

Playfair Cipher

The Playfair cipher is a substitution cipher that encrypts pairs of letters (digraphs) instead of single letters. It was invented in 1854 by Charles Wheatstone and was used by the British in the Boer War and World War I. By working with letter pairs, it avoids some of the weaknesses of simple single-letter substitution.

The Treasure Map Grid Analogy

Imagine a 5×5 grid of letters — like a treasure map. A keyword determines how the grid is arranged. To encrypt, you take your message two letters at a time, find both letters on the grid, and follow three simple rules based on their positions to find the encoded pair. The grid is the key to everything.

Try It

Key Grid

The 5×5 grid built from the keyword. Letters I and J share the same cell (a common convention, since we need 25 cells for 26 letters).

Result

Step-by-Step

Each pair of plaintext letters is transformed according to three rules based on their position in the grid.

How It Works

Building the Grid

  1. Start with your keyword. Remove duplicate letters and treat I and J as the same letter.
  2. Write the keyword letters into a 5×5 grid, left to right, top to bottom.
  3. Fill the remaining cells with the rest of the alphabet (skipping letters already used), still treating I/J as one letter.

Preparing the Message

  1. Remove all non-letter characters and convert to uppercase.
  2. Split the message into pairs of letters (digraphs).
  3. If both letters in a pair are the same (like LL in "HELLO"), insert an X between them: HE LX LO.
  4. If the message has an odd number of letters, add an X at the end to complete the last pair.

The Three Rules

For each pair of letters, find them in the grid and apply the rule that matches their positions:

  1. Same row: Replace each letter with the one to its right (wrapping around to the start of the row if needed). To decrypt, shift left instead.
  2. Same column: Replace each letter with the one below it (wrapping around to the top if needed). To decrypt, shift up instead.
  3. Rectangle: The two letters form opposite corners of a rectangle in the grid. Replace each letter with the letter in the same row but in the other letter's column. (This rule works the same in both directions.)

Why It Is Stronger Than Simple Substitution

A standard substitution cipher replaces one letter at a time, so the letter E always becomes the same ciphertext letter. This makes it vulnerable to frequency analysis.

The Playfair cipher encrypts pairs of letters, so the same letter can produce different ciphertext depending on what letter it is paired with. The pair HE encrypts differently from HA or HO. This makes single-letter frequency analysis useless, though the cipher can still be broken with enough ciphertext by analyzing digraph frequencies.

Things to Try

Disclaimer: these pages are educational demos provided as-is, with no warranty of any kind. The author is not responsible for any consequences arising from their use.

Send comments and bug reports to chris@chrisspackman.com.

Version 0.1 — Last updated: 2026-02-21

This page is Copyright © 2025 – 2026 Chris Spackman <chris@chrisspackman.com>.
This web site developed entirely on GNU/Linux with Free / Open Source Software.

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Creative Commons License