# Playfair Cipher in Cryptography

In this article, we will learn about the **Playfair Cipher technique** which is also a substitutional cipher technique. We will learn what procedure it follows for encryption of the text, and will also see how the encryption is performed?

Submitted by Monika Sharma, on January 08, 2020

**Playfair cipher** is also a substitution cipher technique but it is a bit different than other substitution cipher techniques. Its rules are different. In this cipher, we already know the plain text and the keyword. In this technique, we have to construct a matrix of 5x5 and we have to put each letter of the keyword in that matrix. We should start with the first block of the matrix and fill it horizontally. When the keyword is filled in that matrix then put all the other remaining alphabets in that matrix. The 5x5 matrix is the base structure of this technique. Total blocks in the matrix are 25 and there are 26 alphabets so we have to put **'i'** and **'j'** in the same block.

**Implementation:**

So, to implement the **Playfair cipher technique**, we have to break the plain text in the pair of 2 letters each.

- If the number of alphabets in the message is even then making the pair of 2 letters each.
- If the number of alphabets in the message is odd then take an extra
**'x'**with the last letter of the plain text. - In the pair, if we are getting 2 same alphabets then take
**'x'**with both the alphabets to make pair.

### Finding the Cipher Text

So now we have the plain text broken into pairs of 2 letters each. We have to find that 2 letters in the table individually. Every letter will have a cipher letter for it. The method for finding the Cipher Text is given below,

- If both the letters are in the same column then the ciphertext for both the letters will be the letter just below that letter and if the plain text letter in on the end of the column then the cipher letter for that letter is the first letter of that column.
- If both the letters are in the same row then the ciphertext for both the letters will be the letter just right to that letter and if the plain text letter in on the end of the row then the cipher letter for that letter is the first letter of that row.
- If the plain text letters are neither in the same column nor in the same row then we’ll make a rectangle using the 2 blocks of 2 plain text letters. And in this way, we have 2 more blocks in the rectangle. Now we will take the first letter of the plain text and we’ll traverse its row and the endpoint (another vertex of the rectangle) will be the ciphertext for that letter. Now we’ll take the 2nd letter of plain text and we’ll traverse its row and the endpoint (another vertex of the rectangle) will be the ciphertext letter for that plain text. (We always have to traverse the rows in this case.)

In this way, we can implement the **Playfair Cipher technique**.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.