×

Search anything:

# Play Fair Cipher

#### Software Engineering Play Fair Cipher comes under a Substitution cipher's category and this cipher was the first practical digraph substitution cipher. In play fair cipher unlike traditional cipher, we encrypt a pair/digraph of alphabets (digraphs) instead of a single alphabet.

Sample Test Case:

• Key: "harry"
• Plain Text: "My Name is Ravi"
• Encrypted Text: "SF KBLF MO YRUK"
• Decrypted Text: "MY NAME IS RAVI"

Note:*

1. Assume above test case throughout the discussion/process.
2. Choose characters either be lowercase or uppercase for better conversion and understanding.*

# How Play Fair Works:

Mainly, we have to follow 2 steps which are Generating Key Matrix and Encrypt/Decrypt the Plain/Cipher Text from key matrix.

# Step-1: Generating Key Matrix (Same for Encryption/Decryption)

In Play fair, The Key Matrix will be always 5x5 dimension. It neither depends on key nor on plain text. We have to build always a 5x5 matrix. Now, this 5x5 grid of matrix consists of alphabets(a-z) which is used to encrypt/decrypt the text. Each of the 25 alphabets must be unique and we have to assume I and J as a single char(it's our choice to select I or J). Because, 5x5 grid contains only 25 slots. If the plaintext contains J/I, then it is replaced by I/J(based on our assumption).

Example: If plain Text is "Joker Love" and we're assuming I/J as I then, the text will be "Loker Love".

Now, let's see the steps to build this Key matrix.
First,We have fill the matrix with letters/characters of key without repeating any character(skip duplicates).

H A R Y *
* * * * *
* * * * *
* * * * *
* * * * *

(*) = empty

fill the other slots with remaining characters(a-z) sequentially. Place I/J in one slot(index). Now the resultant will be our Key matrix.

H A R Y B
C D E F G
I/J K L M N
O P Q S T
U V W X Z

# Step-2: Encrypt the Plain Text from Key Matrix

The plain text will split into pairs/digraphs of two letters (digraphs). If there is an odd number of letters, the Z is added to the last letter to make a pair/digraph.

Plain Text: "My Name is Ravi"
After Split: 'my' 'na' 'me' 'is' 'ra' 'vi'

Now, select the pairs/digraphs(plain text) in the key matrix and select the resultant characters according to the below rules.

Rules

1. Form a rectangle with the two letters and take the letters on the horizontal opposite corner of the rectangle.
2. 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).
3. If both the letters are in the same column: Take the letter below each one (going back to the top if at the bottom).

Have a look at below examples:

pair/digraph: my(Rule 3)

H A R Y B
C D E F G
I/J K L M N
O P Q S T
U V W X Z

Resultant Pair/Digraph: SF

pair/digraph: na(Rule 1)

H A R Y B
C D E F G
I/J K L M N
O P Q S T
U V W X Z

Resultant Pair/Digraph: KB

Similarly,

MY -> SF
NA -> KB
ME -> LF
IS -> MO
RA -> YR
VI -> UK

Encrypted key:

'sf'  'kb'  'lf'  'mo'  'yr'  'uk'

# Decryption

In Decryption, We can follow same process for generating key matrix. Instead of Plain text, we will be taking Cipher Text.

Key: "HARRY"
Cipher Text: "SFKBLFMOYRUK"

# Step-2: Decrypt the Cipher Text from Key Matrix

Cipher Text: "SFKBLFMOYRUK"
After Split: 'SF' 'KB' 'LF' 'MO' 'YR' 'UK'

H A R Y B
C D E F G
I/J K L M N
O P Q S T
U V W X Z

Now, select the pairs/digraphs(cipher text) in the key matrix and select the resultant characters according to the below rules.

Rules

1. Form a rectangle with the two letters and take the letters on the horizontal opposite corner of the rectangle.
2. If both the letters are in the same row: Take the letter to the left of each one (going back to the rightmost if at the leftmost position).
3. If both the letters are in the same column: Take the letter above each one (going back to the bottom if at the top).

Have a look at below examples:

pair/digraph: SF (Rule 3)

H A R Y B
C D E F G
I/J K L M N
O P Q S T
U V W X Z

Resultant Pair/Digraph: MY

pair/digraph: KB (Rule 1)

H A R Y B
C D E F G
I/J K L M N
O P Q S T
U V W X Z

Resultant Pair/Digraph: NA

Similarly,

SF -> MY
KB -> NA
LF -> ME
MO -> IS
YR -> RA
UK -> VI

Encrypted key:

'MY'  'NA'  'ME'  'IS'  'RA'  'VI' 