Playfair Cipher in Python
Playfair Cipher in Python
Playfair cipher is a type of polygraphic cipher that uses 5x5 grid of letters to encrypt
and decrypt messages. It was invented by Sir Charles Wheatstone but it is named after
Lord Playfair who promoted its use. The Playfair cipher was widely used in the WW1
and WW2 for military communication.
Key generation
Start with a keyword or phrase, for example: “SECRETKEY”. Write the keyword from
the top left of the 5x5 grid, and fill the rest of the grid with the remaining letters of the
alphabet in order. The grid will look like below:
S E C R T
K Y A B D
F G H I L
M N O P Q
U V W X Z
In the Playfair cipher, ‘I’ and ‘J’ are usually treated as the same letter and occupy the
same cell in the Playfair square. When encrypting or decrypting, you can
interchangeably use ‘I’ and ‘J’ in your plaintext and ciphertext.
Encryption
To encrypt a message, split the message into pairs of letters (digraphs). If both letters
are the same or only one letter is left, then add “X” after the first letter. For example,
lets encrypt the message “WELL DONE MY FRIENDS”. We split them into: “WE LX LD
ON EM YF RI EN DS”.
For each pair of letters, perform the substitution using the following rules:
1. If the letters are in the same row, replace them with the letters to their right
(wrap around if necessary).
2. If the letters are in the same column, replace them with the letters below them
(wrap around if necessary).
3. If the letters are in different rows and columns, replace them with the letters at
the opposite corners of the rectangle formed by them.
We apply the rules above for our message with the 5x5 matrix:
Decryption
To decrypt a message, you will need the same 5x5 grid of letters. For each digraph in
the ciphertext:
1. If the letters are in the same row, replace them with the letters to their right
(wrap around if necessary).
2. If the letters are in the same column, replace them with the letters above them
(wrap around if necessary).
3. If the letters are in different rows and columns, replace them with the letters at
the opposite corners of the rectangle formed by them.
def create_playfair_square(phrase):
key = key.replace('J', 'I').upper() + 'ABCDEFGHIKLMNOPQRSTUVWXYZ'
key = "".join(dict.fromkeys(key)) # Remove duplicate letters
grid = [[k for k in key[i:i+5]] for i in range(0, 25, 5)]
return grid
playfair_square = create_playfair_square('SECRETKEY')
# Output:
# [['S', 'E', 'C', 'R', 'T'],
# ['K', 'Y', 'A', 'B', 'D'],
# ['F', 'G', 'H', 'I', 'L'],
# ['M', 'N', 'O', 'P', 'Q'],
# ['U', 'V', 'W', 'X', 'Z']]
We also define a helper function to get the coordinates of a character from this
Playfair square:
Now that we already have the Playfair square and some helper function, we can write
the function to encrypt messages. Here is the function:
return ciphertext
The function playfair_encrypt takes two parameters: message and key, where
message is the plaintext to be encrypted and key is the phrase to generate the
Fairplay square.
First, the code will generate the Fairplay square using the provided key. After that, it
will pre-process the message to insert ‘X’ between repeating letters. It will also append
‘X’ at the end of the message if the last block only contain a single character.
Inside the loop, each digraph will be replaced with another digraph using the rules
explain in the previous section.
The decryption function is similar with the encryption above, we just don’t need to
pre-process the input message. Instead, we remove the ‘X’ letters between two similar
letters and at the end of the message.
return message
def create_playfair_square(phrase):
"""
Generate the Playfair square for the given phrase.
"""
key = key.replace('J', 'I').upper() + 'ABCDEFGHIKLMNOPQRSTUVWXYZ'
key = "".join(dict.fromkeys(key)) # Remove duplicate letters
return ciphertext
return message
# Usage example
keyword = 'SECRETKEY'
message = 'THE HACKING BLOG ROCKS'
Key Takeaways
To conclude this blog post, let’s review the main points we have covered: