Playfair Cipher in Python
- Cryptography
- Python
Download the PDF version of this post for free:
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.
How the Playfair cipher works?
To encrypt and decrypt messages with Playfair cipher, you need the key which is a 5x5 grid of letters. Then you need to split the message into pairs of letters (digraphs) and replace the characters based on some specific rules.
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:
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:
-
If the letters are in the same row, replace them with the letters to their right (wrap around if necessary).
-
If the letters are in the same column, replace them with the letters below them (wrap around if necessary).
-
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:
-
WE forms a rectangle, replace it with VC.
-
LX forms a rectangle, replace it with IZ.
-
LD is in a column, replace it with QL.
-
ON is in a row, replace it with PO.
-
EM forms a rectangle, replace it with SN.
-
YF forms a rectangle, replace it with KG.
-
RI is in a column, replace it with BP.
-
EN is in a column, replace it with YV.
-
DS forms a rectangle, replace it with KT.
Our final encrypted message is “VCIZQLPOSNKGBPYVKT”.
Decryption
To decrypt a message, you will need the same 5x5 grid of letters. For each digraph in the ciphertext:
-
If the letters are in the same row, replace them with the letters to their right (wrap around if necessary).
-
If the letters are in the same column, replace them with the letters above them (wrap around if necessary).
-
If the letters are in different rows and columns, replace them with the letters at the opposite corners of the rectangle formed by them.
Lets try to decrypt “VCIZQLPOSNKGBPYVKT”. We split them into digraphs “VC IZ QL PO SN KG BP YV KT” and use the previous 5x5 grid to perform the substitutions:
-
VC forms a rectangle, replace it with WE.
-
IZ forms a rectangle, replace it with LX.
-
QL is in a column, replace it with LD.
-
PO is in a row, replace it with ON.
-
SN forms a rectangle, replace it with EM.
-
KG forms a rectangle, replace it with YF.
-
BP is in a column, replace it with RI.
-
YV is in a column, replace it with EN.
-
KT forms a rectangle, replace it with DS.
Combining all of the decrypted digraphs resulting in “WELXLDONEMYFRIENDS”. We can remove the “X” within the two L’s and we got our original message.
Implementing Playfair cipher in Python
Lets start with the function to generate the Playfair square for a given phrase:
The create_playfair_square
function accepts a single argument: phrase
. This phrase will be inserted into the 5x5 grid and the remaining cells will be inserted with the rest of the alphabets.
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:
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.
Putting it all together
Here is the complete code to encrypt/decrypt messages with the Playfair cipher:
Key Takeaways
To conclude this blog post, let’s review the main points we have covered:
-
Playfair cipher is a type of polygraphic encryption technique that uses a 5x5 grid of letters to encrypt and decrypt messages.
-
To generate the Playfair key, you need to choose a keyword or phrase that does not contain any repeated letters, and fill the rest of the grid with the remaining letters of the alphabet in order.
-
You need to split your message into pairs of letters (digraphs), and encrypt or decrypt each pair according to the rules based on their position in the grid.
-
We have shown you how to write python code to encrypt and decrypt text with Playfair cipher in python.
Download the PDF version of this post for free: