Playfair Cipher

Polygraphic substitution cipher

The Playfair cipher encrypts pairs of letters instead of single letters, using a 5×5 table built from a secret keyword. It was invented by Charles Wheatstone and first described in 1854. It is named after the Scottish scientist and politician Lyon Playfair, who popularised its use.

Try the Playfair cipher online ↓

Usage

With the support of Baron Playfair, the cipher was adopted by the British Army. It was used during the Second Boer War, and then in World War I and World War II (also by other countries). Like other ciphers of that period, it went out of use when the first computers appeared. Today it can be broken quickly, for example with brute force attacks.

Algorithm

Playfair is a polygraphic substitution cipher: the plaintext is divided into groups of letters, and each group is replaced by another group. Playfair works on groups of two letters (digraphs).

Building the key table

The table has 5×5 cells and holds 25 letters of the Latin alphabet. The alphabet has 26 letters, so one letter must go: usually I and J are treated as one letter, or a rare letter such as Q or X is dropped.

To fill the table, write the keyword into it, skipping letters that have already appeared. Then fill the remaining cells with the rest of the alphabet in alphabetical order. Both parties must agree on the order in which the cells are filled, usually row by row, left to right, top to bottom.

For example, the keyword pecunia non olet (attributed to the Roman emperor Vespasian), with I and J counted as one letter, gives this table:

pecun
iaolt
bdfgh
kmqrs
vwxyz

Encryption

Split the plaintext into pairs of letters. If a pair would consist of two identical letters, insert a filler letter (usually X) between them and continue from the second letter. If the last letter is left alone, add the filler after it. For example, BALLOON becomes BA LX LO ON.

Then replace each pair using the table:

  1. Different row and column: the two letters mark opposite corners of a rectangle. Replace each letter with the letter in the same row, at the other corner of the rectangle.
  2. Same row: replace each letter with the letter to its right, wrapping around to the start of the row.
  3. Same column: replace each letter with the letter below it, wrapping around to the top of the column.

With the table above, MEET ME AT THE OLD BRIDGE is split into ME ET ME AT TH EO LD BR ID GE and encrypted as WANAW AOIHS CAAGG KABDU. For example, ET is a rectangle (→ NA), AT lies in one row (→ OI), and TH in one column (→ HS).

Decryption

The recipient builds the same table from the keyword and applies the rules in reverse: letters in one row move left, letters in one column move up, and rectangles work exactly as in encryption. Finally, the reader removes filler letters that make no sense in the text.

Playfair cipher online

Letter left out of the table
Direction
Only letters are used; spaces, digits and punctuation are ignored, accents are removed.

Pairs:

Security

The general method of breaking Playfair is frequency analysis of letter pairs. Knowing typical pair frequencies of the message's language, an attacker matches frequent ciphertext pairs to frequent plaintext pairs.

The cipher also has features that make it easier to break. A pair and its reverse (for example AC and CA) always produce a reversed pair in the ciphertext, which helps to recognise common words. And a ciphertext pair never contains two identical letters.

Another method starts with a random table and tries to decrypt the message. The attacker then changes the table slightly and keeps changes that make the decrypted text look more like real language. This hill-climbing approach is simple to implement.

The third, very effective method is guessing parts of the plaintext, such as greetings, dates or places. With the ciphertext and a few probable words, the table can be reconstructed easily. This was a common way of attacking German ciphers of this kind during World War II.

Implementation

The main part of the algorithm is finding the row and column of each letter of a pair. The JavaScript function below transforms one pair; table is the 25-letter table as a string (row by row), and dir is 1 for encryption and -1 for decryption:

function transformPair(table, pair, dir) {
  const i1 = table.indexOf(pair[0]);
  const i2 = table.indexOf(pair[1]);
  const r1 = Math.floor(i1 / 5), c1 = i1 % 5;
  const r2 = Math.floor(i2 / 5), c2 = i2 % 5;
  const at = (r, c) => table[((r + 5) % 5) * 5 + ((c + 5) % 5)];

  if (r1 === r2) return at(r1, c1 + dir) + at(r2, c2 + dir); // same row
  if (c1 === c2) return at(r1 + dir, c1) + at(r2 + dir, c2); // same column
  return at(r1, c2) + at(r2, c1);                            // rectangle
}

The complete code of the tool above, including building the table and splitting the text into pairs, is in playfair.js.