Ciphers

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.
IDfour-square22 / 75classical · digraph
Cipher / Digraph

Four-square

Letter pairs across two plain and two keyed squares.

Keyspace
(25!)² ≈ 2.4×10⁵⁰
Decode
same options back
Works on
A-Z in pairs, J as I (or Q dropped), X pads an odd length, the rest dropped
Family
3 in digraph
required 2 / 3

Options

keystring
required
Keyword for the keyed square at the top right, which gives the first letter
secondKeystring
required
Keyword for the keyed square at the bottom left, which gives the second letter
omitstring
default j
The letter the squares leave out: j folds J into I, q drops Q from squares and text as Wikipedia does

Access

Createcreate("four-square")
CLIciphers encode four-square 'ATTACK AT DAWN' --key EXAMPLE --second-key KEYWORD
Tryplayground with the sample above
Kinplayfair, two-square

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. 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, Polybius and 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 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.