cipher.ink Write a message

Ciphers · Classical ciphers

The two-square cipher

Two keyed squares, one pair of letters at a time. It's Delastelle's reciprocal digraph cipher, and one pair in five passes through unchanged.

The two-square cipher is a digraph cipher in the Playfair family. It encrypts two letters at a time, but with two keyed squares, one for each letter of the pair, where Playfair uses one. It's also called double Playfair. It's reciprocal, so the same steps with the same keys decrypt, and it's easy to do by hand. It has a flaw Playfair doesn't: about one pair in five goes through unchanged.

Try it

Nothing you type here leaves your browser.

The tool uses the "vertical" layout, with one square above the other. There's no Decrypt button. Paste the result back in with the same two keys.

How it works

Build two 5×5 squares from two keywords, as in Playfair, leaving out the letter Q so 25 letters fit. Stack them, one above the other, and cut the message into pairs. Find the first letter of the pair in the top square and the second in the bottom square. Those two letters are opposite corners of a rectangle that spans both squares, and the other two corners give the ciphertext pair. The first ciphertext letter sits in the top square, in the row of the first plaintext letter and the column of the second. The second ciphertext letter sits in the bottom square, in the row of the second plaintext letter and the column of the first.

Two-square: LP becomes DLEXAMPLBCDFGHIJKNORSTUVWYZKEYWORDABCFGHIJLMNPSTUVXZfirst lettersecond letter
LP becomes DL. L is in the top square and P in the bottom one; D and L are the other corners of the rectangle they define.

Run the message HELP ME OBI WAN KENOBI with the keys EXAMPLE and KEYWORD and you get HEDLXWSDJYANHOTKDG. The pair LP becomes DL, as in the picture. The same operation turns HEDL… back into HELP…, because swapping the rectangle's corners twice returns you to the start.

Here it is by coordinates. Build the top square from EXAMPLE (EXAMP / LBCDF / GHIJK / NORST / UVWYZ) and the bottom square from KEYWORD (KEYWO / RDABC / FGHIJ / LMNPS / TUVXZ). For the pair LP, L is in the top square at row 2, column 1, and P is in the bottom square at row 4, column 4. The first ciphertext letter is in the top square at the row of L (2) and the column of P (4), which is D. The second is in the bottom square at the row of P (4) and the column of L (1), which is L. So LP becomes DL.

The transparent pairs

When the two letters of a pair lie in the same column, the rectangle collapses and the ciphertext pair equals the plaintext pair. The example shows it twice: HE and AN come out as HE and AN. With letters spread over five columns, about one pair in five (20%) is one of these transparencies, and anyone who reads the ciphertext can see it. A codebreaker who spots the word HE or AN in the ciphertext knows it's probably the same in the plaintext. In an 18-letter message that's a useful crack. In a long message it hands over a fifth of the text.

History

The two-square and its relatives are credited to Félix Delastelle, a French amateur cryptographer, who described them along with the four-square, bifid and trifid ciphers in Traité élémentaire de cryptographie, published in 1902. In the Second World War the German forces used a related system called Doppelkastenschlüssel, "double-box key", also known as Double Playfair. The British cryptanalysts at Bletchley Park broke it.

Breaking it

  • Look for transparencies. Pairs that pass through unchanged show up as the same pair in a guessed word and in the ciphertext. The more of them you find, the fewer squares are consistent.
  • Use a probable word. A guessed word such as REPORT gives you a few known pairs. Each pair constrains where letters sit in the two squares, and you can follow the constraints like a puzzle.
  • Hill climbing. A computer starts from random squares, decrypts, scores the text with four-letter statistics, and keeps small changes that improve the score, as in the solver for Playfair.

The number of key pairs, 25! × 25!, about 2.4 × 1050, is enormous. As with all these ciphers, though, the size of the key space says almost nothing about how long breaking it takes.

The code

One pair through the two-square rectangle. The same function decrypts.

// Two-square, vertical arrangement. `top` and `bottom` are 25-letter keyed squares (Q left out), row by row.
// The first letter of a pair is found in the top square, the second in the bottom one; the ciphertext pair is the other two
// corners of the rectangle. Running it again with the same squares decrypts.
function twoSquarePair(top, bottom, a, b) {
  var ia = top.indexOf(a), ib = bottom.indexOf(b);
  var ra = Math.floor(ia / 5), ca = ia % 5, rb = Math.floor(ib / 5), cb = ib % 5;
  return top[ra * 5 + cb] + bottom[rb * 5 + ca];
}

Common questions

What's the difference between two-square and Playfair?

Playfair uses one square for both letters of a pair, and two-square uses a different square for each. That's why two-square pairs can come through unchanged, and why a Playfair ciphertext never has a double letter while a two-square one can.

What does "transparent digraph" mean?

A pair of plaintext letters that encrypts to itself. In the vertical two-square it happens whenever both letters lie in the same column of the squares, so about one pair in five is transparent.

Is the two-square cipher reciprocal?

Yes. Encrypting the ciphertext with the same two squares returns the plaintext, so the tool has no Decrypt button.

Why is Q left out of the squares?

A 5×5 square holds 25 letters and the alphabet has 26. Either I and J share a cell (as in Playfair) or Q is dropped (as in the usual two-square examples). Q is rare and easy to restore when you read the result.

What's Double Playfair?

Another name for the two-square cipher. The German forces used a related system in the Second World War, and Bletchley Park broke it.

Further reading

  1. Wikipedia: Two-square cipher
  2. Wikipedia: Two-square cipher