cipher.ink Write a message

Ciphers · Classical ciphers

The Polybius square

Each letter becomes a row number and a column number. It started as torch signals in ancient Greece and ended up as prison tap codes.

The Polybius square turns each letter into two numbers, its row and its column in a 5×5 grid. The Greek historian Polybius described it around 150 BC as a way to signal with torches, and it's the ancestor of the tap codes prisoners use and of the ADFGVX cipher of the First World War. On its own it's a simple substitution. It changes the symbols and leaves the statistics alone.

Try it

Nothing you type here leaves your browser.

With no keyword the square is the plain alphabet, with I and J sharing a cell. Add a keyword and it goes first, which scrambles the square. To decrypt, choose Decrypt and paste the numbers.

How it works

Write the 25 letters in a 5×5 grid, with rows and columns numbered 1 to 5. Each letter is replaced by its row number followed by its column number. In the plain square, P sits in row 3, column 5, so P is 35.

P is row 3, column 51234512345ABCDEFGHIKLMNOPQRSTUVWXYZ
The Polybius square. P is 35: row 3, column 5.

POLYBIUS becomes 35 34 31 54 12 24 45 43 32, and Meet at dawn becomes 32 15 15 44 11 44 14 11 52 33. A grid of 25 has one cell too few for the English alphabet, so I and J share a cell (some versions merge C and K). A 6×6 grid takes the letters and ten digits, and ADFGVX uses that.

Torches on a hillside

Polybius (about 200–118 BC), a Greek who spent years in Rome, described the square in Book 10 of his Histories and credited earlier inventors, Cleoxenus and Democleitus. His problem was long-distance signaling. A fire beacon could say only what had been agreed in advance, such as "the enemy is coming". With the square, a signaler could spell anything. He held up a number of torches in his left hand for the row and a number in his right for the column, and the watcher on the next hill read them off. The Greek alphabet has 24 letters, so a 5×5 square fits it with one spare cell, which is probably why the square has that shape.

It was a signaling system, not a secret code, because anyone who could see the torches and knew the square could read them. Its value as cryptography came later, when other people noticed that numbers are easy to hide.

Where it turns up

  • The tap code. Prisoners tap a row and a column on a wall or pipe, with a pause between the two numbers. Prisoners of war in Vietnam used a version of the Polybius square, with C and K sharing a cell, to talk through the walls of their cells.
  • Nihilist cipher. Russian revolutionaries imprisoned in the nineteenth century used a Polybius square followed by adding a keyword's numbers, which turns it into a polyalphabetic cipher.
  • ADFGVX. The German Army's 1918 cipher began with a Polybius square, labeled with the letters A, D, F, G, V and X instead of numbers, and then scrambled the result with a transposition (ADFGVX).
  • Bifid. Delastelle's cipher splits each letter's row and column and mixes them (bifid cipher).

Breaking it

On its own, a Polybius square is a simple substitution. Each plaintext letter becomes one pair of digits, always. Read the digits in pairs and you have 25 symbols whose counts match English letter counts, so the usual method applies. A keyword changes which letter sits where, but there are only 25! squares, about 1.55 × 1025, and no frequency is hidden. The substitution solver works on it once you map each digit pair to a letter.

The idea's strength is as a first step. Replacing letters with coordinates makes them fractionated: every letter becomes two symbols, and those two symbols can then be shuffled, added to something or transposed. That's what makes ADFGVX and bifid much harder than Polybius alone.

The code

The plain Polybius square: letters to pairs of digits and back.

// Polybius square: each letter becomes its row and column, 1 to 5 (I and J share a cell).
var POLYBIUS = 'ABCDEFGHIKLMNOPQRSTUVWXYZ';
function polybius(text, decode) {
  if (decode) {                                             // pairs of digits back to letters
    var d = text.replace(/[^1-5]/g, ''), out = '';
    for (var i = 0; i + 1 < d.length; i += 2) out += POLYBIUS[(d[i] - 1) * 5 + (d[i + 1] - 1)];
    return out;
  }
  return text.toUpperCase().replace(/[^A-Z]/g, '').replace(/J/g, 'I').split('').map(function (c) {
    var i = POLYBIUS.indexOf(c);
    return (Math.floor(i / 5) + 1) + '' + (i % 5 + 1);
  }).join(' ');
}

Common questions

Why do some Polybius squares merge I and J?

A 5×5 grid has 25 cells and the alphabet has 26 letters, so two letters have to share one. I and J are the usual choice, since J was seldom used in older Latin text, but C and K, or Q alone, turn up too.

Is the Polybius square a cipher or a code?

It's a way of writing letters as numbers. Alone it's a simple substitution and gives little secrecy, but it's the first step in several stronger ciphers, such as ADFGVX and the bifid.

How does the tap code relate to it?

The prisoners' tap code is a Polybius square: tap the row, pause, tap the column. The usual version puts C and K in one cell and leaves out K.

Can I use a 6×6 Polybius square?

Yes. A 6×6 square holds all 26 letters and 10 digits. ADFGVX uses one, with the rows and columns labeled with letters instead of numbers.

How would I break a keyed Polybius square?

Read the digits in pairs and treat each pair as one letter, and it becomes an ordinary monoalphabetic substitution. The substitution solver handles it after you map each pair to a letter.

On the timeline

  • c. 150 BC Polybius describes the 5 by 5 square, credited to Cleoxenus and Democleitus.

Further reading

  1. Polybius: Histories, book 10 (LacusCurtius)
  2. Wikipedia: Polybius square
  3. Wikipedia: Tap code