---
title: "Four-square"
description: "Delastelle's four 5×5 squares, two plain and two keyed. Letter pairs go across the corners, and doubled letters come out the other side."
canonical_url: "https://ciphers.agntn.dev/ciphers/four-square"
---
# Four-square

> Delastelle's four 5×5 squares, two plain and two keyed. Letter pairs go across the corners, and doubled letters come out the other side.

<cipher-facts name="four-square">



</cipher-facts>

Félix Delastelle published this one in 1902. Four 5×5 squares sit in one big square. Top left and bottom right hold the plain alphabet. The other two are keyed. Find the first letter of a pair top left, the second bottom right. The other two corners of that rectangle are the ciphertext. Twice Playfair's keys, and one rule instead of three.

```ts
const fourSquare = create("four-square");
const squares = { key: "zgptfoihmuwdrcnykeqaxvsbl", secondKey: "mfnbdcrhsaxyogvituewlqzkp" };
fourSquare.encode("attack at dawn", squares).text; // "TIYBFHTIZBSY"
fourSquare.decode("TIYBFHTIZBSY", squares).text; // "ATTACKATDAWN"
```

That's the [example on Practical Cryptography](http://practicalcryptography.com/ciphers/classical-era/four-square/). Its keys are whole squares, 25 letters each. A short keyword works too.

## Two keys

`key` fills the square at the top right, the one that gives the first letter of each pair. `secondKey` fills the bottom left. Both build like Playfair's table: the keyword's letters, repeats dropped, then the rest of the alphabet. Both are required. A missing one is a `MissingOptionError`, one without a single ASCII letter an `InvalidOptionError`.

Same keyword twice? Allowed. But then `AB` and `BA` come out as mirror images, just like in Playfair. That's the one tell four-square was supposed to hide.

## J or Q?

25 cells, 26 letters. Something has to go. By default J folds into I, as in [Playfair](https://ciphers.agntn.dev/ciphers/playfair), [Polybius](https://ciphers.agntn.dev/ciphers/polybius) and [Bifid](https://ciphers.agntn.dev/ciphers/bifid). Wikipedia drops Q instead. Feed its `EXAMPLE` and `KEYWORD` example to the default squares and it agrees on the first pair, then wanders off:

```ts
const keys = { key: "EXAMPLE", secondKey: "KEYWORD" };
fourSquare.encode("help me obi wan kenobi", keys).text;
// "FYNFNEHWBXAFFOKHMD"
fourSquare.encode("help me obi wan kenobi", { ...keys, omit: "q" }).text;
// "FYGMKYHOBXMFKKKIMD", Wikipedia's ciphertext
```

`omit: "q"` builds all four squares without Q. J gets its own cell back, and a Q in the text or the keywords simply drops out. Q barely pulls its weight in English anyway. Mostly it just drags a U around. Neither choice is wrong. Different squares, different ciphertext.

Got a four-square that decodes into mush? Flip `omit` before you blame the keys. The result options carry `omit` only when it's `q`, so a default call reports the two keys and nothing else.

## No X between doubles

The text loses everything but letters and comes out in capitals. An odd length gets an X at the end, and that's the only X. Playfair has to split the `LL` in `WALL`. Four-square doesn't, because the two letters sit in different squares:

```ts
fourSquare.encode("defend the east wall of the castle", squares).text;
// "FBUMCNESFDPIKKZXCXMIUNZNQUNM"
```

See the `KK`? Playfair never writes a pair like that. Decoding keeps the padding X, since only the sender knows it was padding. Ciphertext with an odd number of letters is a `CipherError`.

## Spotting one

No J, an even count, letters only. So far it looks like Playfair. Then a doubled pair shows up and Playfair is out. [`guess`](https://ciphers.agntn.dev/guide/analysis) names four-square on that. Bifid leaves the same traces, so the two come back side by side, bifid first. Squares without Q put J back into the ciphertext, and `guess` doesn't look for that one yet.

Want to settle it by hand? Count the pairs at even positions. A four-square keeps the plaintext's `TH` and `HE`, only renamed, so the same pairs keep coming back. Bifid stirs its letters across five at a time and loses them.


## Sitemap

See the full [sitemap](https://ciphers.agntn.dev/sitemap.md) for all pages.
