Playfair Cipher is a digraph substitution cipher where a 5×5 matrix of alphabets is created according to the key. It is a symmetric cipher where there is only one key for encryption and decryption.
Example 1:
Message: IAMLEARNING
Key: KIDSLABZ
We have to split the message by pairing two characters: IA, ML, EA, RN, IN, GX [X is added as only 1 character remains]
5×5 matrix according to the key:
In this matrix, character ‘J’ is omitted; instead, it is replaced by character ‘I’.
Encryption:
Take character pair from splitted message and check the position of each character in the 5×5 matrix.
If the characters lie in the same row, then the encrypted message will be one character right from both characters. [Move to the left, if the character is in the rightmost column]
If the characters lie in the same column, then the encrypted message will be one character below both characters. [Move to the top, if the character is in the bottommost row]
If the characters lie in different rows and columns, the encrypted message will be the horizontal opposite corner of the rectangle formed by the two characters.
IA = They lie in different rows and columns, so the encrypted message will be KB
ML = They lie in different rows and columns, so the encrypted message will be NS
EA = They lie on the same row, so the encrypted message will be AB
RN = They lie in different rows and columns, so the encrypted message will be TM
IN = They lie in different rows and columns, so the encrypted message will be LG
GX = They lie in different rows and columns, so the encrypted message will be MV
Final encrypted message = KBNSABTMLGMV
Example 2:
Message: PROGRAMMING
Splitted Message: PR, OG, RA, MX [X is added because pair cannot be made with the same letter ], MI, NG
Encryption:
PR = They lie on the same row, so the encrypted message will be QT
OG = They lie in different rows and columns, so the encrypted message will be PF
RA = They lie in different rows and columns, so the encrypted message will be OC
MX = They lie on the same column, so the encrypted message will be RS
MI = They lie in different rows and columns, so the encrypted message will be GS
NG = They lie on the same row, so the encrypted message will be FH
Final encrypted message: QTPFOCRSGSFH
The implementation of the Playfair Cipher in Python is given below:
# Python program to implement Playfair Cipher
message = "IAMLEARNING"
# message = "PROGRAMMING"
key = "KIDSLABZ"
new_message = ""
splitted_msg = []
matrix = []
encrypted_msg = ""
# Function to split the message and group into 2 characters
def split_message():
global new_message
# Checking if there is any similar characters adjacent to each other and generate new message
for i in range(0, len(message), 2):
# Checking for single character remaining
if len(message[i:i+2]) < 2:
new_message = new_message + message[i]
elif message[i] == message[i+1]:
new_message = new_message + message[i] + "X" + message[i+1]
else:
new_message = new_message + message[i] + message[i+1]
# Splitting the new message
for i in range(0, len(new_message), 2):
temp = new_message[i:i+2]
if len(temp) < 2:
temp += "X"
splitted_msg.append(temp)
# Function to generate the 5x5 matrix
def generate_matrix():
checked_char = key
for i in range(5):
row = []
for j in range(5):
# Adding characters of key into the matrix
if 5*i+j < len(key):
row.append(key[5*i+j])
else:
# Adding other alphabets into the matrix
for k in range(65, 91):
if chr(k) not in checked_char:
if chr(k) == 'J':
continue
row.append(chr(k))
checked_char += chr(k)
break
matrix.append(row)
# Function to get location of character in the matrix
def getLocation(c):
for i in range(5):
for j in range(5):
if matrix[i][j] == c:
return i,j
# Function to generate encrypted characters
def encryption(x1,y1,x2,y2):
char1 = ""
char2 = ""
# If the characters lies in the same row
if x1 == x2:
char1 = matrix[x1][(y1 + 1)%5]
char2 = matrix[x2][(y2 + 1)%5]
# If the characters lies in the same column
elif y1 == y2:
char1 = matrix[(x1 + 1)%5][y1]
char2 = matrix[(x2 + 1)%5][y2]
# If the characters lies in the different row and column
else:
char1 = matrix[x1][y2]
char2 = matrix[x2][y1]
return char1, char2
# Calling the functions to split message and generate matrix
split_message()
generate_matrix()
# Finding the locations of characters from splitted message and generate encrypted characters
for i in range(len(splitted_msg)):
x1, y1 = getLocation(splitted_msg[i][0])
x2, y2 = getLocation(splitted_msg[i][1])
char1, char2 = encryption(x1, y1, x2, y2)
encrypted_msg = encrypted_msg + char1 + char2
# Printing the data
print("Message: ", message)
print("New Message: ", new_message)
print("Splitted Message: ", splitted_msg)
print("Key: ", key)
print("Generated Matrix: ")
for i in range(5):
print(matrix[i])
print("Encrypted Message: ", encrypted_msg)
Continue reading “Implementation of Playfair Cipher in Python”