If you haven’t read this article on Playfair Encryption, go take a look first! It has a brief on the Playfair cipher as well as the logic behind most of the code.

Contents

  1. The Playfair Cipher
  2. Rules for Decryption
  3. C Implementation
  4. Outputs for Some Ciphertexts
  5. Further Reading

The Playfair Cipher

Assuming the keyword is ‘_Charles’, _the decryption procedure would be as follows. A 5x5 matrix is drawn, and letters are filled in each cell, starting with the keyword, followed by the letters in the alphabet. I/J are filled in the same cell. All repeating letters are removed, giving us this matrix -

Given a ciphertext sentence, it is split into digrams. For the ciphertext ‘_gddogdrqprsdhmembv’, _the digrams would be -

gd do gd rq pr sd hm em bv

Rules for Decryption

  1. Two ciphertext letters in the same row of the matrix are each replaced by the letter to the left, with the last element of the row circularly following the first.

sd would be replaced by eb

gi would be replaced by ng

2. Two ciphertext letters that fall in the same column of the matrix are replaced by the letters above, with the bottom element of the column circularly following the top.

my would be replaced by dt

yr would be replaced by ty

3. Otherwise, each ciphertext letter in a pair is replaced by the letter that lies in its own row and the column occupied by the other ciphertext letter.

gd would be replaced by me

do would be replaced by et

Following these rules, the plaintext becomes ‘Meet me at the bridge’ (x is neglected at the end since its a filler letter).

C Implementation

First we import the required libraries and define a large enough size for allocation of the ciphertext to be decrypted.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#define SIZE 100

Now we write a function to decrypt a ciphertext using the Playfair cipher. The string is converted to lower case and the 5x5 key square is generated. For this, first a 26 character hashmap is used to store the count of each alphabet in the key string. Using this, each cell in the matrix is populated with the key string alphabets first, and only once by reducing the count in the hashmap. Then the remaining alphabets are populated.

Each character in the ciphertext is then searched for in the digraph and its position is found. Based on the relative positions of characters in a pair, following the above detailed rules, decryption is performed, and the decrypted pair is returned. Once the digrams are joined, a sentence can be made by looking at the letters and neglecting filler letters like x, etc.

The main difference between the encryption and decryption implementation are the corner cases; when alphabets are in the first row or first column. In the encryption case, alphabets in the last row or last column had to be replaced by those in the first, and this code was taken care of by a modulus 5 operation. However, for decryption, separate if clauses have to be written for these corner cases.

#cryptography #crypto #c #playfair-cipher #decryption

Playfair Cipher Decryption
2.20 GEEK