# 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'

• It is somewhat hard to crack this algorithm since the frequency analysis(brute-force technique) used to crack simple substitution ciphers which is difficult but still can work on digraphs(all combinations of digraphs) rather than 25 monographs.
• The plaintext is found by solving one set of clues, while the ciphertext is found by solving others. Solvers can then construct the key table by pairing the digraphs (it is sometimes possible to guess the keyword).
• But, A weakness is that a digraph of a ciphertext/plaintext will have corresponding plaintexts. So That can be easily used to find with the frequency analysis technique, if the language of the plaintext is known.
• Another disadvantage is that playfair cipher is a symmetric cipher, So same key will be used for both encryption and decryption.

With this article at OpenGenus, you must have the complete idea of Play Fair Cipher. Enjoy. 