cipher.ink Write a message

Ciphers · Classical ciphers

The four-square cipher

Delastelle's four-square cipher encrypts letter pairs with two plain squares and two keyed ones. A worked example, and how it's attacked.

The four-square cipher encrypts letter pairs using four 5×5 squares: two ordinary alphabets and two scrambled by keywords. Félix Delastelle designed it as a stronger Playfair, and it fixes the two-square cipher's habit of leaving some pairs unchanged. It's still a pencil-and-paper cipher, and a computer breaks it.

Try it

Nothing you type here leaves your browser.

Use Decrypt with the same two keys to reverse it.

How it works

Arrange four squares in a 2×2 grid. The top-left and bottom-right squares hold the ordinary alphabet (Q left out so that 25 letters fit). The top-right and bottom-left squares are filled from two keywords. To encrypt a pair, find the first plaintext letter in the top-left square and the second in the bottom-right square. They sit at opposite corners of a rectangle that spans the whole arrangement. The other two corners are the ciphertext, the first from the top-right square and the second from the bottom-left.

Four-square: HE becomes FYplain square (H)ABCDEFGHIJKLMNOPRSTUVWXYZkey square 1 (first letter)EXAMPLBCDFGHIJKNORSTUVWYZkey square 2 (second letter)KEYWORDABCFGHIJLMNPSTUVXZplain square (E)ABCDEFGHIJKLMNOPRSTUVWXYZ
HE becomes FY. H in the top-left square and E in the bottom-right square define a rectangle; the keyed squares supply the other two corners.

With the keywords EXAMPLE and KEYWORD, the message HELP ME OBI WAN KENOBI becomes FYGMKYHOBXMFKKKIMD. To decrypt, run the rectangle the other way: find the two cipher letters in the keyed squares (the first in the top-right, the second in the bottom-left) and read the plaintext corners from the plain squares.

It works in both directions because every ciphertext pair has one letter in each keyed square, so no pair can pass through unchanged. That removes the 20% leak of the two-square cipher.

By hand, step by step

Build the keyed squares first. Write the keyword EXAMPLE, drop the repeated E, and fill in the rest of the alphabet without Q. The square reads EXAMP / LBCDF / GHIJK / NORST / UVWYZ. The second keyword KEYWORD gives KEYWO / RDABC / FGHIJ / LMNPS / TUVXZ. The two plain squares are ABCDE / FGHIJ / KLMNO / PRSTU / VWXYZ.

Now encrypt HE. In the top-left plain square, H is in row 2, column 3. In the bottom-right plain square, E is in row 1, column 5. The first ciphertext letter sits in the top-right keyed square at the row of H (2) and the column of E (5), and that's F. The second sits in the bottom-left keyed square at the row of E (1) and the column of H (3), and that's Y. So HE becomes FY, as in the picture. Every pair goes through the same two lookups, so a 20-letter message takes ten steps and a steady hand. Decrypting is the same walk in reverse. Find the two cipher letters in the keyed squares, mark the rectangle with the plain squares, and read off the plaintext corners. The common slip is taking the corners from the wrong pair of squares, so check the labels in the picture before each lookup.

History

Félix Delastelle (1840–1902), a Frenchman, published his ciphers in Traité élémentaire de cryptographie in 1902, shortly after his death. The four-square sits with his bifid, trifid and two-square ciphers. Cipher hobbyists know them well, and the American Cryptogram Association has run puzzles with them for decades. No major military or diplomatic use of the four-square is widely documented.

Breaking it

Every plaintext pair has an equal chance of landing on any ciphertext pair that fits the keys, but letters aren't equally likely, so English leaks through. The standard attacks:

  • Known plaintext. If you know or can guess a few words, each pair constrains the layout of the keyed squares, and a modest crib is enough to rebuild both squares by hand. The US Army's manual of basic cryptanalysis, FM 34-40-2, describes the method.
  • Digraph statistics. Search over the two keyed squares, trying to bring the decrypted pair frequencies in line with English. It's the hill-climbing technique used against Playfair.
  • Single-letter patterns. Both plain squares use the ordinary alphabet, so a given plaintext letter in a given position always lands in a fixed row or column of one of the keyed squares. Repeated patterns can then be matched to known words.

The key space is two keyed squares: (25!)2, about 2.4 × 1050. That's far too large to search blindly and small enough to climb.

The code

Four-square for one pair of letters, encrypting or decrypting.

// Four-square. `plain` is the ordinary alphabet square (Q left out); k1 and k2 are the two keyed squares.
// Encrypting: the first letter is looked up in the top-left plain square, the second in the bottom-right one.
// The cipher letters are the other corners: row of the first with column of the second, taken from k1; and vice versa, from k2.
function fourSquarePair(plain, k1, k2, a, b, decode) {
  if (!decode) {
    var ia = plain.indexOf(a), ib = plain.indexOf(b);
    return k1[Math.floor(ia / 5) * 5 + ib % 5] + k2[Math.floor(ib / 5) * 5 + ia % 5];
  }
  var ja = k1.indexOf(a), jb = k2.indexOf(b);               // decrypting runs the same rectangle backwards
  return plain[Math.floor(ja / 5) * 5 + jb % 5] + plain[Math.floor(jb / 5) * 5 + ja % 5];
}

Common questions

Why four squares?

Two are the ordinary alphabet and two are keyed. The plain alphabets are where you find the letters, and the keyed ones give the answer, so every ciphertext pair gets scrambled and there are no unchanged pairs as in the two-square.

Is the four-square cipher reciprocal?

No. Encryption and decryption use the rectangle in different directions, so the tool has a Decrypt switch.

Does four-square have two keywords?

Yes, one for the upper-right square and one for the lower-left. Two keywords make more keys than Playfair's single square.

Why is the first plaintext letter looked up in a different square from the second?

So the rectangle spans the two plain squares and lands in the keyed squares, which is where the output letters come from. Looking both letters up in one square would give Playfair.

Who uses the four-square cipher today?

Mostly puzzle makers and codebreaking hobbyists, for example in the American Cryptogram Association's puzzles. It has no serious security value.

Further reading

  1. Wikipedia: Four-square cipher
  2. US Army: FM 34-40-2, Basic Cryptanalysis (1990)
  3. Practical Cryptography: Four-square cipher