T

Text Machine

Powerful text tools, in your browser

Playfair Cipher

Encode and decode the Playfair cipher with a keyword of your choice. Switch between the I/J and no-Q square variants, follow along on the interactive 5×5 key square, and copy, download, or share the result. Everything runs in your browser.

Keyword

I and J share one cell, so the square holds 25 letters. Any J in your text is treated as I. This is the most common convention.

Plain text
Ciphertext

Enter text above to see the result here.

Key square (5×5)

The keyword fills the grid first, in highlighted cells, then the rest of the alphabet completes it. Every digraph is enciphered by locating its two letters on this square.

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

How to use Playfair Cipher

  1. 1

    Choose encode or decode

    Pick Encode to turn plain text into Playfair ciphertext, or Decode to turn ciphertext back into the prepared plain text using the same keyword.

  2. 2

    Pick a square variant

    Choose the common I/J variant, where I and J share a cell, or the no-Q variant, where Q is left out. A short hint explains how each one builds the 25-letter square.

  3. 3

    Enter your keyword

    Type a keyword such as MONARCHY. Its distinct letters fill the 5×5 key square first, and the highlighted cells below update instantly to show your grid.

  4. 4

    Type or paste your text

    Enter your message and the cipher runs as you type. Letters are paired and enciphered, doubled pairs are split with a filler, and spaces, digits, and punctuation are removed.

  5. 5

    Copy, download, or share

    Copy the result, download it as a text file, or share a link that reopens the tool with your exact keyword, text, and settings ready to go.

Understanding the Playfair Cipher

What is the Playfair cipher?

The Playfair cipher is a manual symmetric encryption technique and the first cipher in history to encrypt pairs of letters instead of single letters. Working on digraphs, two-letter blocks, rather than one letter at a time, flattens the single-letter frequency pattern that breaks simple substitution ciphers, so for its era Playfair was both strong and quick enough to use by hand in the field.

It was invented by the British scientist Charles Wheatstone in 1854 but takes its name from his friend Lord Playfair, who promoted its use to the British government and military. Today it is a classic teaching cipher and a favourite in puzzles, escape rooms, and capture-the-flag challenges, because it is rich enough to be interesting yet simple enough to work by hand.

Building the 5×5 key square

Everything in Playfair revolves around a 5×5 grid of 25 letters built from a secret keyword. You write the distinct letters of the keyword into the grid first, skipping any repeats, and then fill the remaining cells with the rest of the alphabet in order. Because 26 letters cannot fit into 25 cells, one letter has to share or be dropped: in the most common convention I and J share a single cell, while an alternative convention simply leaves Q out.

The keyword scrambles the alphabet into an order only someone with the keyword can reproduce, and the tool above highlights the keyword letters so you can see exactly how your grid is laid out. With the keyword MONARCHY, for example, the first row becomes M O N A R, and the alphabet then continues from where the keyword leaves off.

The three Playfair rules

Before enciphering, the message is split into pairs of letters. If a pair would be a double letter, such as the LL in BALLOON, a filler letter X is inserted to break it up, and if the message has an odd number of letters a final filler is added so every pair is complete. Spaces, digits, and punctuation are removed first, because there is nowhere to put them on the square.

Each pair is then enciphered with one of three rules based on where its two letters sit. If both letters are in the same row, each is replaced by the letter immediately to its right, wrapping around to the start of the row. If both are in the same column, each is replaced by the letter immediately below, wrapping to the top. Otherwise the two letters form the corners of a rectangle, and each is replaced by the letter in its own row at the other letter's column.

A worked Playfair example

Take the keyword PLAYFAIR EXAMPLE and the message HIDE THE GOLD IN THE TREE STUMP. The key square becomes P L A Y F on the first row, then I R E X M, B C D G H, K N O Q S, and T U V W Z. The message splits into the pairs HI DE TH EG OL DI NT HE TR EX ES TU MP, where an X has been inserted to break the double E in TREE.

The first pair HI forms a rectangle, so H becomes B and I becomes M, giving BM. Working through every pair the same way produces the ciphertext BM OD ZB XD NA BE KU DM UI XM MO UV IF. Decrypting it with the same keyword reverses each rule and returns HIDETHEGOLDINTHETREXESTUMP, the prepared message with its inserted X still in place.

Decrypting a Playfair cipher

Decryption uses the very same key square and simply runs the rules backwards. For two letters in the same row you move left instead of right, and for the same column you move up instead of down; the rectangle rule is its own mirror image, so it is unchanged. Choose Decode above, enter the keyword that was used to encrypt, and paste the ciphertext to recover the message.

One quirk is worth remembering: because letters are folded together, doubled pairs are split, and odd messages are padded, the decrypted text is the prepared message rather than the exact original. You will sometimes see a stray X or Z between repeated letters or at the very end, and a J that was merged into I stays as I. Readers remove these by eye, which is normal for Playfair and not a fault of the tool.

A short history of the Playfair cipher

Charles Wheatstone first described the cipher in 1854, and Lord Playfair demonstrated it to senior figures including Prince Albert and the future Prime Minister Lord Palmerston. The British War Office at first thought it too complicated, but its real strength was that it needed no tables or apparatus, only a memorised keyword, which made it ideal for officers in the field.

It saw genuine service for decades. British forces used it in the Second Boer War and in the First World War, and it was still employed for low-level traffic in the Second World War, including by Australian coastwatchers in the Pacific. Its value was tactical: a message only needed to stay secret for the hours or days during which the information it carried still mattered.

How secure is the Playfair cipher?

By modern standards Playfair is not secure, but it is far tougher than a simple substitution cipher. Because it enciphers 600 possible digraphs rather than 26 letters, a plain frequency count of single letters gives nothing away, and an attacker must instead analyse the frequencies of letter pairs. With a few hundred characters of ciphertext, known techniques such as digraph frequency analysis and hill-climbing with simulated annealing can recover the key.

That makes Playfair a wonderful educational and recreational cipher: strong enough to show why pair-based encryption was a real advance, yet weak enough to break as a puzzle. For any genuine need to protect information you should use a modern, peer-reviewed algorithm such as AES instead.

Frequently asked questions

What is the Playfair cipher?
The Playfair cipher is a manual encryption technique that enciphers pairs of letters using a 5×5 key square built from a keyword. It was the first cipher to encrypt letters two at a time rather than one at a time, which hides the single-letter frequency pattern that breaks simpler ciphers. Invented by Charles Wheatstone in 1854, it is named after Lord Playfair, who promoted it.
How does the Playfair cipher work?
You build a 5×5 grid from a keyword, then split your message into pairs of letters. Each pair is enciphered by where its letters sit on the grid: same row means shift right, same column means shift down, and otherwise the pair forms a rectangle and you swap to the letters in the same rows but the opposite columns. Decryption uses the same grid with the directions reversed.
What is the 5×5 key square?
The key square is a grid of 25 letters that drives the whole cipher. You write the distinct letters of the keyword into it first, then fill the rest with the remaining alphabet in order. Because 26 letters do not fit in 25 cells, I and J share a cell in the usual convention, or Q is dropped in the alternative. The square below the tool updates live and highlights your keyword letters.
What are the three Playfair rules?
For each pair of letters: if they are in the same row, replace each with the letter to its right, wrapping around. If they are in the same column, replace each with the letter below, wrapping around. If they are in neither, they form a rectangle, and each letter is replaced by the one in its own row at the other letter's column. Decryption moves left and up instead, while the rectangle rule stays the same.
Why does Playfair insert X or Z between letters?
A pair of identical letters cannot be enciphered, so a filler is inserted to break it up, which is why a word like BALLOON gains an X between its two L letters. The filler is normally X, but when the letter beside it is already X the tool uses Z instead so the inserted letter is always different. A lone final letter is padded the same way so that every pair is complete.
What is the difference between the I/J and no-Q variants?
Both squeeze the 26-letter alphabet into 25 cells, just differently. In the I/J variant the letters I and J share one cell, so any J in your text is enciphered as I. In the no-Q variant the letter Q is left out of the square entirely, so any Q in your text is skipped. The I/J convention is the most common, but both appear in textbooks and puzzles, so the tool supports each.
Can you show a worked Playfair example?
With the keyword PLAYFAIR EXAMPLE, the message HIDE THE GOLD IN THE TREE STUMP becomes the pairs HI DE TH EG OL DI NT HE TR EX ES TU MP and enciphers to BM OD ZB XD NA BE KU DM UI XM MO UV IF. An X was inserted to split the double E in TREE. Decoding the ciphertext with the same keyword returns the prepared message HIDETHEGOLDINTHETREXESTUMP.
How do you decrypt a Playfair cipher?
Use the same keyword and key square that encrypted the message, then run the rules in reverse. Same-row pairs move left, same-column pairs move up, and the rectangle rule is unchanged. Choose Decode above, enter the keyword, and paste the ciphertext. The result is the prepared message, so you may see filler letters that the original reader would simply ignore.
Who invented the Playfair cipher?
It was invented by Charles Wheatstone, a British scientist also known for work on the telegraph, in 1854. It is named after his friend Lyon Playfair, the first Baron Playfair, who championed it to the British government and military. The cipher saw real use in the Second Boer War and the First World War, and for low-level messages into the Second World War.
How do you break or crack the Playfair cipher?
Single-letter frequency analysis does not work because Playfair enciphers pairs, so attackers study the frequencies of digraphs instead. With a few hundred characters of ciphertext, methods such as digraph frequency analysis and automated hill-climbing with simulated annealing can recover the keyword. A short message with little repetition is much harder to break, which is part of why Playfair was useful in the field.
Does the Playfair cipher keep spaces, numbers, and punctuation?
No. Playfair works only on the letters of the alphabet, because there is nowhere on the 5×5 square for anything else. Before enciphering, the tool removes spaces, digits, and punctuation and converts everything to uppercase. This is why decrypted Playfair text comes back as a continuous run of letters rather than tidy words and sentences.
Is my text uploaded to a server?
No. All encoding and decoding happens entirely in your browser, so your text and keyword are never uploaded, logged, or stored. Even a share link keeps your text and keyword in the part of the URL after the hash, which browsers never send to a server, so it stays private until you choose to share it.

Related tools

Keep going with these handy tools

Four-Square Cipher

Two-Square Cipher

Caesar Cipher

Vigenère Cipher

Atbash Cipher

Rail Fence Cipher