Javatpoint Logo
Javatpoint Logo

Playfair Cipher Implementation in Python

The Playfair cipher is a polygraphic substitution cipher that was invented by Sir Charles Wheatstone in 1854. It uses a 5x5 grid of letters (usually called the key square) to perform encryption and decryption. The key square is constructed from a keyword, where the keyword letters are first arranged in the key square in order, and then the remaining letters of the alphabet are filled in, excluding 'j' (which is usually treated the same as 'i').

The step-by-step explanation is as follows:

  • Key Generation: In the first step, a 5x5 matrix is generated from a given key, known as the "keysquare". The key is first converted to uppercase and any spaces are removed. After that, the letters of the key are added to the key square, making sure to eliminate any duplicates. The remaining letters of the alphabet are arranged in order and then added to the key square to fill in the rest of the matrix.
  • Encryption: The intended message is first changed to uppercase and any spaces are eliminated. If the length of the message is odd, a "X" is added to the end to make it even. After that, the message is divided into pairs of letters, and each pair is encrypted using the key square. The encryption is performed as follows: if the two letters are in the same row, the letters to the right of them in the key square are used. The letters in the same column are used to the letters in the key square below. The letters in the key square that are in the same row as the first letter and in the same column as the second letter, and vice versa, are used if the letters are in different rows and columns.
  • Decryption: Decryption is simply the reverse of encryption. The ciphertext is decrypted and transformed back into the original plaintext using the same key square.

For Example:

A complete python code implementation for Playfair Cipher encryption and decryption:


  • Time complexity: The Playfair cipher has a time complexity of O(n^2), where n is the length of the message. It is because each digraph in the message must be encrypted or decrypted by looking up its row and column indices in the key square, which requires iterating over the key square for each digraph. It can make the cipher slow for large messages.
  • Key size: The key size of the Playfair cipher is relatively small, as it uses a 5x5 key square that can be filled with 25 distinct letters. However, the key space is further reduced by the fact that 'j' is usually treated as the same as 'i', and by the rules for filling in the remaining letters of the alphabet. It means that the effective key size is closer to 23 or 24 letters. Additionally, the security of the cipher depends on the secrecy of the keyword, which can be vulnerable to known-plaintext and chosen-plaintext
  • Security: The Playfair cipher is considered relatively weak by modern standards, as it can be easily broken by known-plaintext and chosen-plaintext Additionally, the cipher is vulnerable to frequency analysis attacks, as the frequencies of digraphs in the ciphertext can reveal information about the plaintext. However, the cipher can be made more secure by using a longer keyword, using a more secure padding scheme, and using additional layers of encryption.
  • Implementation details: The exact implementation of the Playfair cipher can affect its security and robustness. For example, different rules can be used for handling repeated letters in -plaintext, or for handling odd-length Additionally, some implementations use additional layers of encryption, such as a transposition cipher or a permutation of the key square. Careful testing and validation are important to ensure that the implementation is secure and robust against known attacks.

It is not recommended for use in modern applications due to its relative weakness and limited key space.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA