The Four-Square Cipher
Learn about the four-square cipher, a digraph substitution cipher that uses four 5x5 grids to encrypt letter pairs without Playfair's awkward same-row and same-column special cases.
Interactive Four-Square Cipher Visualizer
🔐 Four-Square Cipher Visualizer
The Four-Square Cipher: Two Keys, Four Grids
Introduction
The Playfair cipher encrypts letter pairs using a single 5×5 grid, but that single-grid design forces three separate rules depending on whether the two letters share a row, share a column, or neither. The four-square cipher, invented by the French cryptographer Félix Delastelle in the late 19th century, takes a different approach: it uses four 5×5 grids instead of one. That eliminates Playfair’s special cases entirely. Every digraph is encrypted with exactly the same rule, every time.
Table of Contents
- How the Four-Square Cipher Works
- The Encryption Rule
- A Worked Example
- Decryption
- Four-Square vs. Playfair
- Pros and Cons Analysis
- Cryptanalysis and Breaking the Four-Square Cipher
- Modern Relevance
- Python Implementation
- Limitations
- Conclusion
- FAQ
- References
How the Four-Square Cipher Works
The four-square cipher arranges four 5×5 grids in a 2×2 layout:
Plain (top-left) Key 1 (top-right)
Key 2 (bottom-left) Plain (bottom-right)
The top-left and bottom-right grids both hold the plain alphabet in standard order (I and J sharing a cell, exactly like the Polybius square). They’re never keyed. The top-right and bottom-left grids each hold a keyed alphabet, built from two independent keywords the same way a Playfair key square is built. Because there are two independent keyed grids, the four-square cipher effectively has two separate keys to manage, rather than Playfair’s single keyword.
As with every 5×5-grid cipher in this series, only 25 cells are available for 26 letters, so I and J conventionally share one cell. And as with Playfair, an odd-length message gets a filler letter (conventionally X) appended to the end before splitting into digraphs, so every letter has a partner. Spaces and punctuation are stripped out entirely before that splitting happens.
Interactive Visualizer
Try it above with the default keys “EXAMPLE” and “KEYWORD”. Watch each plaintext digraph highlighted in the two plain grids, then the resulting ciphertext digraph highlighted in the two keyed grids.
The Encryption Rule
Unlike Playfair, four-square has only one rule, applied identically to every digraph: no same-row, same-column, or rectangle cases to remember:
- Find the first plaintext letter’s position in the top-left (Plain) grid.
- Find the second plaintext letter’s position in the bottom-right (Plain) grid.
- The first ciphertext letter is read from the top-right (Key 1) grid, at the row of the first letter and the column of the second letter.
- The second ciphertext letter is read from the bottom-left (Key 2) grid, at the row of the second letter and the column of the first letter.
In effect, the two plaintext letters’ row and column coordinates get “crossed” between the two keyed grids to produce the ciphertext pair. Schematically, for a digraph “HE”: if H sits at (row 2, column 3) in the Plain grid and E sits at (row 1, column 5), the ciphertext pair is Key1[row 2, col 5] followed by Key2[row 1, col 3]. Each keyed grid borrows one letter’s row and the other letter’s column.
A Worked Example
Using Key 1 = “EXAMPLE” and Key 2 = “KEYWORD”, here are the four grids the visualizer above builds:
Plain (top-left) Key 1 (top-right, from "EXAMPLE")
A B C D E E X A M P
F G H I K L B C D F
L M N O P G H I K N
Q R S T U O Q R S T
V W X Y Z U V W Y Z
Key 2 (bottom-left, from "KEYWORD") Plain (bottom-right)
K E Y W O A B C D E
R D A B C F G H I K
F G H I L L M N O P
M N P Q S Q R S T U
T U V X Z V W X Y Z
Now encrypt “HELLO” (padded to “HELLOX” for an even length, split into digraphs HE, LL, OX) using these four grids:
Digraph “HE”: H sits at row 2, column 3 in the Plain grid; E sits at row 1, column 5. Crossing them: the first ciphertext letter is Key 1’s row 2, column 5. Key 1’s row 2 is “L B C D F”, and its 5th entry is F. The second ciphertext letter is Key 2’s row 1, column 3. Key 2’s row 1 is “K E Y W O”, and its 3rd entry is Y. Reading row 2 of Key 1 and row 1 of Key 2 at the crossed columns gives ciphertext “FY”.
Digraph “LL”: Both L’s sit at row 3, column 1 in the Plain grid. Crossing coordinates between Key 1 and Key 2 gives ciphertext “GF”.
Digraph “OX”: O sits at row 3, column 4; X sits at row 5, column 3. Crossing coordinates gives ciphertext “IX”.
“HELLOX” → “FYGFIX”
Notice that “LL,” a repeated letter, needed no special handling at all. That’s unlike Playfair, where a repeated letter within a digraph forces inserting a filler letter to split the pair. Four-square’s crossed-grid rule works identically regardless of whether the two letters match.
Decryption
Decryption reverses the same crossing rule, just starting from the keyed grids instead of the plain ones:
- Find the first ciphertext letter’s position in the Key 1 grid.
- Find the second ciphertext letter’s position in the Key 2 grid.
- The first plaintext letter is read from the Plain (top-left) grid at the row of the first ciphertext letter and the column of the second.
- The second plaintext letter is read from the Plain (bottom-right) grid at the row of the second ciphertext letter and the column of the first.
Applying this to “FYGFIX” with the same two keywords recovers “HELLOX” exactly. The trailing “X” is the padding filler added before encryption, visible in the recovered plaintext just as it would be with Playfair.
Four-Square vs. Playfair
Both are classic digraph substitution ciphers from the same conceptual family, but they differ in structure:
- Playfair: One keyed 5×5 grid, one keyword, three different rules depending on the digraph’s row/column relationship, and a required filler letter for repeated-letter digraphs.
- Four-square: Two plain (unkeyed) grids plus two independently keyed grids, one single crossed-coordinate rule for every digraph, and no special handling needed for repeated letters.
Four-square’s uniform rule makes it slightly easier to implement correctly, since there are no edge cases to get wrong. That comes at a cost, though: managing two keywords instead of one, and needing four full grids instead of one to describe the cipher completely.
Pros and Cons Analysis
| Advantages | Disadvantages |
|---|---|
| One Uniform Rule: No same-row/same-column/rectangle special cases to remember or implement incorrectly, unlike Playfair | Two Keys to Manage: Requires securely sharing and remembering two independent keywords instead of one |
| No Special Handling for Repeated Letters: A digraph like “LL” encrypts exactly like any other pair, simplifying both implementation and manual use | Still a Digraph Substitution Cipher: Vulnerable to digraph-frequency analysis, the two-letter analogue of single-letter frequency analysis, given enough ciphertext |
| Genuinely Stronger Than Playfair: A larger effective key space (two keywords instead of one) and no exploitable structural quirks from the special-case rules | More Complex to Set Up by Hand: Building and referencing four grids is more cumbersome than Playfair’s single grid |
| Historically Well-Regarded: Used seriously for manual field cryptography well after Playfair, precisely because it avoided Playfair’s known weaknesses | No Real-World Security Today: Like every cipher in this series, offers no protection against modern automated cryptanalysis |
Cryptanalysis and Breaking the Four-Square Cipher
Four-square resists frequency analysis better than plain substitution ciphers for the same reason Playfair does: it operates on digraphs (letter pairs) rather than individual letters. So simple single-letter frequency counts don’t directly apply. But digraph-level frequency analysis still works in principle: common English digraphs (TH, HE, IN, ER) still occur more often than rare ones. With enough ciphertext, statistical patterns in the encrypted digraphs can be matched against known digraph frequency tables.
In practice, four-square is somewhat more resistant to manual cryptanalysis than Playfair specifically because it lacks Playfair’s structural regularities (the same-row and same-column special cases create exploitable patterns that trained cryptanalysts learned to recognize). But against modern computational cryptanalysis, which can brute-force or statistically attack the relatively small combined keyspace of two keyed 5×5 grids, it offers no meaningful protection.
Modern Relevance
Four-square has no place in modern security, but remains valuable as:
- A refinement case study: a clear example of how a cipher’s inventor (Delastelle) identified a specific weakness in an existing design (Playfair’s special cases) and engineered a structural fix.
- A natural follow-up to Playfair in any classic-cryptography curriculum, showing that digraph substitution didn’t stop evolving after Playfair’s initial design.
- A puzzle and recreational cryptography format, still occasionally used in puzzle-hunt and codebreaking communities that enjoy classic ciphers.
Python Implementation
The two plain grids never need building since they’re always the standard alphabet; only the two keyed grids need generating. The crossed-coordinate rule from earlier is then a single lookup per letter:
ALPHABET = 'ABCDEFGHIKLMNOPQRSTUVWXYZ' # 25 letters, I/J share a cell
def generate_square(key):
key = key.upper().replace('J', 'I')
key = ''.join(c for c in key if c.isalpha())
square = []
seen = set()
for char in key + ALPHABET:
if char not in seen:
seen.add(char)
square.append(char)
return square
def find_position(square, char):
if char == 'J':
char = 'I'
return divmod(square.index(char), 5)
def prepare_digraphs(text):
text = text.upper().replace('J', 'I')
text = ''.join(c for c in text if c.isalpha())
if len(text) % 2 != 0:
text += 'X'
return [text[i:i + 2] for i in range(0, len(text), 2)]
def foursquare_encrypt(text, key1_square, key2_square):
result = ''
for pair in prepare_digraphs(text):
r1, c1 = find_position(ALPHABET, pair[0])
r2, c2 = find_position(ALPHABET, pair[1])
result += key1_square[r1 * 5 + c2]
result += key2_square[r2 * 5 + c1]
return result
def foursquare_decrypt(ciphertext, key1_square, key2_square):
result = ''
for i in range(0, len(ciphertext), 2):
r1, c1 = find_position(key1_square, ciphertext[i])
r2, c2 = find_position(key2_square, ciphertext[i + 1])
result += ALPHABET[r1 * 5 + c2]
result += ALPHABET[r2 * 5 + c1]
return result
if __name__ == "__main__":
key1_square = generate_square("EXAMPLE")
key2_square = generate_square("KEYWORD")
plaintext = "HELLO"
ciphertext = foursquare_encrypt(plaintext, key1_square, key2_square)
recovered = foursquare_decrypt(ciphertext, key1_square, key2_square)
print(f"Plaintext: {plaintext}")
print(f"Ciphertext: {ciphertext}")
print(f"Recovered: {recovered}")
This reproduces the worked example above exactly: HELLO pads to HELLOX, then encrypts to FYGFIX, and decrypts back to HELLOX, the trailing filler still visible on the way out, exactly as the Decryption section above describes.
Limitations
This implementation mirrors the visualizer’s scope:
- No same-key protection. Nothing stops
generate_squarefrom being called with two identical keywords, which would quietly forfeit the “two independent keys” advantage described above without any warning. - Filler ambiguity, same as Playfair. A decrypted message can end with a genuine
Xor a paddingX, and the code has no way to tell them apart. - English letters only, formatting is lost. Spaces, punctuation, and case are stripped before splitting into digraphs, same as every 5×5-grid cipher in this series.
- Not intended for real security. As the Cryptanalysis section above notes, digraph-frequency analysis still applies given enough ciphertext; this code demonstrates the crossed-coordinate mechanism, not a secure system.
Conclusion
The four-square cipher shows what happens when a cryptographer takes a working design (Playfair) and asks “what if the special cases weren’t necessary at all?” By splitting the single keyed grid into four grids (two plain, two keyed), every digraph follows the exact same crossed-coordinate rule, with no exceptions to memorize and no repeated-letter edge case to handle. It’s a genuine structural improvement over Playfair, even though, like every classic cipher in this series, it offers no real protection against modern cryptanalysis.
FAQ
What is the four-square cipher?
A digraph substitution cipher, invented by Félix Delastelle, that uses four 5×5 grids (two plain, two independently keyed) to encrypt letter pairs by crossing their row and column coordinates between the plain and keyed grids.
How is four-square different from Playfair?
Playfair uses one keyed grid and three different rules depending on whether the digraph’s letters share a row, share a column, or neither. Four-square uses two plain grids and two keyed grids with a single uniform crossed-coordinate rule, and needs no special handling for repeated letters.
Why doesn’t four-square need a filler letter for repeated letters like Playfair does?
Its rule operates independently on each letter’s row and column via separate grids, rather than relying on relative row/column relationships within a single grid. So a digraph like “LL” is processed exactly like any other pair, with no ambiguity.
Is the four-square cipher more secure than Playfair?
Somewhat. It has a larger combined keyspace (two keywords) and avoids the structural regularities Playfair’s special-case rules create. But it’s still a digraph substitution cipher, vulnerable to digraph-frequency analysis and completely insecure against modern computational cryptanalysis.
Who invented the four-square cipher?
Félix Delastelle, a French cryptographer, also known for inventing the related Bifid and Trifid ciphers, which apply a similar “split the coordinates apart” idea in a different way.
References
-
Wikipedia. “Four-square cipher.” Available at: https://en.wikipedia.org/wiki/Four-square_cipher
-
Practical Cryptography. “Four-Square Cipher.” Available at: http://practicalcryptography.com/ciphers/four-square-cipher/
-
Singh, Simon. “The Code Book.” Doubleday, 1999.