What is Playfair Cipher Encryption Algorithm
Tags: security, python, programming
Published: 2021-09-30 20:37:36

This is going to be two or three part series where I'm going to explain the theory behind the Playfair cipher and how we can program it using most probably everyone's favourite language 🥁🥁🥁 (*"DRUM ROLLS"*)  **Python**, Yay!!

<p>&nbsp;</p>
# Playfair cipher encryption Algorithm:
>The Playfair cipher or Playfair square or Wheatstone–Playfair cipher is a manual symmetric encryption technique and was the first literal digram substitution cipher. The scheme was invented in 1854 by Charles Wheatstone, but bears the name of Lord Playfair for promoting its use. - Source: Wikipedia

<p>&nbsp;</p>

So what we can grasp from above is that, this algorithm was developed by Charles Wheatstone but bears the name as Lord Playfair promoted it. Also we can depict that it was first practical digraph substitution cipher.

> **Digraph Cipher** : It simply means that we encrypt the plaintext to ciphertext with two characters at a time. (Will get more idea as I explain you through.)  

<center> * * * * * * *</center>
## The Playfair Cipher Algorithm
The Algorithm mainly consist of three steps: 
1. Convert plaintext into digraphs (i.e., into pair of two letters)
2. Generate a Cipher Key Matrix
3. Encrypt plaintext using Cipher Key Matrix and get ciphertext.

<p>&nbsp;</p>

>*For the rest of our discussion we will be using below mentioned Key and Plaintext:*
**Key** = DevToDiesel
**Plaintext** = Awesome Dev Community


So let's move forward!!!
 
<p>&nbsp;</p>
## Step:1 Convert plaintext into digraphs (i.e., into pair of two letters)
So our Plaintext `Awesome Dev Community` will be 
```
[AW, ES, OM, ED, EV, CO, MX, MU, NI, TY] 
```

Now You might be wondering why `MX` instead of `MM` and where did the whitespaces go! Because there are three rules to be followed here and those are:

1.While splitting text into pairs, if the letters are same in a pair then insert filler x. 

2.At the end if only one letter is left it has no pair we can insert our filler x

3.Remove Whitespaces from the string as well as special characters apart from 25 alphabets.

<p>&nbsp;</p>
<center> * * * * * * *</center>
<p>&nbsp;</p>
## Step:2 Generate a Cipher Key Matrix

Cipher Key Matrix is a 5×5 grid of alphabets that acts as the key for encrypting the plaintext.

Each of the 25 alphabets must be unique and one letter of the alphabet (usually J) is omitted from the table (as the table can hold only 25 alphabets). 

If the plaintext contains J, then it is replaced by I.

*As you might expected! There are also rules to generate this key matrix. You're getting brainy now!*

### Rules: 

**1. If our key has repeating letters omit them.**
So our Key = "DevToDiesel" Will be
```
D E V T O I S L
```
<p>&nbsp;</p>

**2. Fill this unique characters key in our 5X5 Matrix**
So it might look something like this:
![Half Mat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgrif2fq5zro15h7ck9x.png)
 
 
<p>&nbsp;</p>


**3. Fill the remaining places with unused alphabets without J** As it should only contain 25 letters, only omitting J would result in matrix like this:


![Full Mat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtui4hbfoieyswnshv0d.png)
 
 

<p>&nbsp;</p>
<center> * * * * * * *</center>
<p>&nbsp;</p>

## Step 3: Encrypt plaintext using Cipher Key Matrix and get ciphertext.

Now we will encrypt our diagraph plaintext that is
```
[AW, ES, OM, ED, EV, CO, MX, MU, NI, TY]
``` 
into Cipher text with the help of cipher key matrix, and following below listed rules.
<p>&nbsp;</p>

### Rule: 1
If both letters are not in same column and not in same row then draw a imaginary rectangle shape and take letters on the horizontal opposite corner of the rectangle.


**Example:**
Our first pair from plaintext is "AW", so A will be enciphered to S and W will enciphered to Y.
i.e., 
1. A -> S
2. W -> Y
resulting in,**AW -> SY**.

![Rule 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ev1lp2r8v7q5dqmdbnfp.png)
 

Similarly, pair OM will be enciphered to DR.
i.e., **OM -> DR**


![Rule 1.2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o1lbkr77y02gugevlye.png)
 


 
<p>&nbsp;</p>

### Rule: 2
If both the letters are in the same column: Take the letter below each one (going back to the top if at the bottom).


**Example** 
Our Second pair is "ES", so according to above rule it will be enciphered to its below character in same column.
i.e., 
1. E -> S
2. S -> F
thus, **ES -> SF**.


![Rule 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29f3of9b4b48zkir46ym.png)
 


<p>&nbsp;</p>

### Rule: 3 
If both the letters are in the same row: Take the letter to the right of each one (going back to the leftmost if at the rightmost position)

**Example** 
The pair DE is in same row, so just choose the right next to each character from cipher key matrix.
i.e., 
1. D -> E
2. E -> V
so, **DE -> EV**

![Rule 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3j5t5cg7esa6m9dbt1f.png)
 

<p>&nbsp;</p>
<center> * * * * * * *</center>
<p>&nbsp;</p>

> Same way after performing above rules for whole the list of diagraph plain text, our cipher text will be generated.

**Plaintext**
```
[AW, ES, OM, ED, EV, CO, MX, MU, NI, TY]
```

**Ciphertext**
```
[SY, SF, DR, VE, VT, KD, PU, UD, MS, AT]
```

Plaintext = **AWESOMEDEVCOMXMUNITY**
Ciphertext = **SYSFDRVEVTKDPUUDMSAT**

<center> * * * * * * *</center>

### In the next part of series I'll be showing how to code playfair cipher using python, so stay tuned.

**Don't forget to leave your thoughts and doubts in discussion section below, also drop a heart ❤ if you gained some knowledge!**

Thanks for reading out!😀😀