# Playfair Cipher

Learn about some improvements that can help prevent single letter frequency analysis and how the Playfair cipher incorporates them in its design.

We'll cover the following

## Introduction

The Playfair cipher is an unusual cryptosystem because it operates on pairs of letters (bigrams). The Playfair cipher consists of a preprocessing stage followed by an encryption stage.

### Preprocessing for the Playfair cipher

The plaintext requires a degree of preprocessing before it can be encrypted. We now describe this process and explain why each step is necessary:

1. Replace Js with Is: The Playfair cipher is based on a five-by-five square grid (the Playfair square) with 25 positions, each of which contains a different letter of the alphabet. Thus, one letter of the alphabet can’t appear in this grid and must be discarded. It makes the most sense for this to be a fairly uncommon letter. There’s nothing special about the selection of the letter J, other than the fact that it doesn’t occur often in English. The choice of replacement of the letter J by the letter I is because of the superficial resemblance between these two letters. A number of variants of the Playfair cipher could be designed that remove a different unpopular letter and replace it with another of the remaining letters.

2. Write out the plaintext in pairs of letters: This is done because the Playfair cipher operates on bigrams, so it processes the plaintext in ‘blocks’ of two letters.

3. Split pairs of identical letters by inserting a Z between them: The Playfair cipher relies on the two letters in each plaintext bigram being different. If a plaintext bigram currently consists of two identical letters, then the letter Z is inserted between them in the plaintext in order to separate them. There’s nothing special about the selection of the letter Z. Any other unpopular letter could be chosen instead. Indeed, if two Zs occur together in a bigram of the plaintext, then some other letter must be inserted between them.

4. Rewrite the modified plaintext in pairs of letters: Check to see whether there has been a ripple effect that has resulted in a new plaintext bigram consisting of two identical letters. If this is the case, then repeat the previous step until there are no such bigrams.

5. If the final number of letters is odd, then add Z to the end: This is done in order to guarantee that the entire plaintext can be partitioned into bigrams. If there’s an odd number of letters, then the last letter needs a partner in order to be processed. Again, Z is used simply because it’s an unpopular letter in English. If the last plaintext letter is itself a Z, then a different final character will need to be used.

### Encryption using the Playfair cipher

The Playfair square, which forms the key of the Playfair cipher, consists of a five-by-five grid, where each entry contains a unique letter of the alphabet other than J. Having preprocessed the plaintext according to the outlined procedure, the plaintext is encrypted as follows:

1. If two plaintext bigram letters lie in the same row of the Playfair square, then replace each letter with the letter on its right in the Playfair square. If one of the letters lies in the fifth entry of the row, then replace it with the letter in the first entry of the row.

2. If two plaintext bigram letters lie in the same column of the Playfair square, then replace each letter with the letter beneath it in the Playfair square. If one of the letters lies in the fifth entry of the column, then replace it with the letter in the first entry of the column.

3. If the letters in a bigram do not lie either in the same row or the same column of the Playfair square, follow the following rules:

• Replace the first plaintext bigram letter with the letter in the Playfair square occurring in the same row as the first bigram letter and the same column as the second bigram letter.

• Replace the second plaintext bigram letter with the letter in the Playfair square occurring in the same column as the first bigram letter and the same row as the second bigram letter.

Get hands-on with 1200+ tech skills courses.