DEV Community

Cover image for What is Playfair Cipher Encryption Algorithm
Karan-Munjani
Karan-Munjani

Posted on • Updated on

What is Playfair Cipher Encryption Algorithm

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!!

 

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

 

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.)

* * * * * * *

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.

 

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!!!

 

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] 
Enter fullscreen mode Exit fullscreen mode

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.

 

* * * * * * *

 

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
Enter fullscreen mode Exit fullscreen mode

 

2. Fill this unique characters key in our 5X5 Matrix
So it might look something like this:
Half Mat

 

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

 

* * * * * * *

 

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]
Enter fullscreen mode Exit fullscreen mode

into Cipher text with the help of cipher key matrix, and following below listed rules.

 

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

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

Rule 1.2

 

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

 

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

 

* * * * * * *

 

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]
Enter fullscreen mode Exit fullscreen mode

Ciphertext

[SY, SF, DR, VE, VT, KD, PU, UD, MS, AT]
Enter fullscreen mode Exit fullscreen mode

Plaintext = AWESOMEDEVCOMXMUNITY
Ciphertext = SYSFDRVEVTKDPUUDMSAT

* * * * * * *

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!😀😀

Top comments (5)

Collapse
 
suyashdev profile image
Suyash Vashishtha

There is a typo in Step 2 Rule 1... New key will be DEVTOISL... there is an extra E in new key..

Collapse
 
karanmunjani profile image
Karan-Munjani

Oh! Thanks @suyashvash for pointing out.

✅Changes are done!!

Collapse
 
0xjepsen profile image
Waylon Jepsen

Really Great Content! We need more cryptography resources, up to and beyond lattice based crypto systems.

Collapse
 
karanmunjani profile image
Karan-Munjani

Thank You, Waylon!😄

Indeed it's crucial to save our data from future quantum attacks.

Collapse
 
singhabhishek6 profile image
singhabhishek6

just checking how post works here tring to cline it