Four-square
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, X pads an odd length, the rest dropped
- Family
- 2 in digraph
Options
Access
- Create
create("four-square") - CLI
ciphers encode four-square 'ATTACK AT DAWN' --key EXAMPLE --second-key KEYWORD - Tryplayground with the sample above
- Kinplayfair
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.
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. Here J folds into I, as in Playfair, Polybius and Bifid. Wikipedia drops Q instead. So its EXAMPLE and KEYWORD example agrees on the first pair and then wanders off:
fourSquare.encode("help me obi wan kenobi", { key: "EXAMPLE", secondKey: "KEYWORD" }).text;
// "FYNFNEHWBXAFFOKHMD", where Wikipedia prints FYGMKYHOBXMFKKKIMD
Neither is wrong. Different squares, different ciphertext. A puzzle built on squares without Q won't read here.
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:
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.
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.