# What the f*ck is Playfair Cipher? - A Complete Tutorial

## Playfair Cipher operates on pairs rather than individual letters, making it more resistant to frequency analysis attacks.

Cryptography is securing communication by converting plain text into a coded form, making it unintelligible to unauthorized individuals. One such cryptographic technique is the Playfair Cipher, invented by Charles Wheatstone in 1854.

The Playfair Cipher operates on pairs rather than individual letters, making it more resistant to frequency analysis attacks. It employs a square grid, known as a key square, to determine the substitution of letters in plaintext.

The Playfair Cipher uses a 5 × 5 matrix of letters (the key table), which contains no duplicates. The letters I and J are treated as the same letter. We form the key table by placing the unique letters of a keyword in order, followed by the remaining letters of the alphabet.

### 1 . Security 🔐

Let us take an example to understand how Playfair Cipher works. The first word we will see is *SECURITY*.

First, we write down the letters of this word in the first squares of the key matrix:

Then, we fill up the remaining squares of the matrix with the remaining letters of the alphabet, following the alphabetical order. However, since there are 26 letters and only 25 squares available, we assign both I and J to the same square:

Here, we cannot see the letter J, but it is assigned in the same box as I.

When selecting a keyword, it’s vital to ensure that no letter is repeated and especially that the letters I and J don’t appear together. For instance, keywords such as INJURE, JUICE, and JIGSAW wouldn’t be suitable due to the violation of this rule, as they include both I and J simultaneously.

Now that we have the key matrix, let us encrypt our word SECURITY.

### 2. Pre-processing ✨

Before encrypting the actual message, we must preprocess it. We treat J as I (effectively excluding the former from the encryption process). Also, we remove any non-alphabetic characters, such as spaces or punctuation marks.

We move forward by splitting the prepared message into pairs of letters (digraph). If a digraph contains identical consecutive letters, we insert X between them. Additionally, if the plaintext has an odd length, we append X at the end to form a complete digraph.

For example, when dealing with the word HELLO WORLD, we divide the message into pairs of letters:**HE LL OW OR LD**

Since the digraph LL contains identical consecutive letters, we insert X between them:**HE LX LO WO RL D**

As the message has an odd length after insertion, we append X at the end to make it even:**HE LX LO WO RL DX**

Now that we have processed the text, we can start our encryption process.

### 3. Encryption Rules 📏

To apply encryption on our processed text, we must follow three important rules-

If both letters in the pair are in the same row of the key square, we replace each letter with the letter to its right (wrapping around if necessary).

If both letters in the pair are in the same column of the key square, we replace each letter with the letter below it (wrapping around if necessary).

If the letters are in different rows and columns, we form a rectangle with the pair and replace each letter with the letter at the rectangle’s opposite corner (moving only left or right).

Using the matrix with the keyword SECURITY, let’s find the row and column of each pair and apply the encryption rules to HELLOWORLD, whose pairs are HE LX LO WO RL DX.

First, we encrypt the pair 'H E'. These are in different rows and different columns. Hence, the third rule is applied. We replace both the letters with elements of the **same row**, however, the element must belong to a **different column**. This different column is determined by the column of the pair alphabet.

So, we know that the row of H is 3rd. Hence, element that replaces H will belong in the 3rd row. Next, we know that column of E is 2nd. Hence, the element that replaces H will belong to the second column. The element that belongs to the 3rd row and 2nd column is F. Hence, H gets replaced with F.

Similarly, E gets replaced by U, since we have to take the element on the same row as E and same column as H.

We can do the same for the second pair, which is LX. L gets replaced with O and X with Q. Now, let's take the third pair which is L O. Here, the second rule gets applied. The elements get replaced by elements to their right. So, L gets placed with M and O with P.

We can similarly perform encryption for all the other letters. After applying the encryption rules to all the letter pairs, we get FUOQMPXNSPHQ.

### 4 . Decryption Rules 🎧

Decrypting a message encrypted using Playfair Cipher is almost the same, with every rule opposite to the encryption rules.

When both letters in a pair are located in the same row of the key square, we replace each letter with the letter to its left, considering wrapping around if necessary.

Similarly, suppose both letters in the pair are situated in the same column of the key square. In that case, we replace each letter with the letter above it, also considering wrapping around it if necessary.

When the letters are in different rows and columns, we form a rectangle using the letter pair and replace each letter with the letter at the rectangle’s opposite corner.

Let’s decrypt the message FUOQMPXNSPHQ using these decryption rules. The pairs are FU OQ MP XN SP HQ.

F and U are in different rows and columns, so they form a rectangle with corners E, U, F, and H. By exchanging F with its opposite corner H and U with its opposite corner E, we get HEOQMPXNSPHQ

O and Q are in different rows and columns, so they form a rectangle with corners L, O, X, and O. By exchanging O with its opposite corner L and Q with its opposite corner X, we get HELXMPXNSPHQ

Continuing this process, we get HELXLOWORLDX.

Now, we can do the post-processing of text.

### 5 . Post Processing 🎷

Post processing of text have two rules-

If X appears between two same letters, then remove it.

If X appears at the end of the word, then remove it.

Following these two rules, our message becomes HELLOWORLD.

### 7 . Conclusion 🪙

The Playfair cipher offers the advantage of employing polygram substitution, which involves encrypting two letters together instead of single-letter substitution. This characteristic enhances the encryption complexity, significantly raising the difficulty level for potential attackers attempting to decrypt the message without possessing the key. Thus, the Playfair cipher’s use of polygram substitution is its key strength.

The Playfair cipher has certain drawbacks. Firstly, it relies on a 5 by 5 matrix of letters, making it unable to encrypt numbers, symbols, or non-alphabetic characters. This limitation restricts its usefulness for messages that include a wider range of characters. The relatively small key space and the patterns present in the encryption make it susceptible to statistical analysis attacks.

Additionally, it doesn’t offer perfect secrecy since the frequency distribution of the original message is preserved in the ciphertext. Analyzing the frequencies of the ciphertext makes it possible to deduce the original message, given enough ciphertext.

Therefore, the Playfair cipher is a simple algorithm, but its limitations and vulnerabilities to various cryptanalysis techniques make it less suitable for secure communication in modern contexts: