PlayFair Cipher in C#

In my previous article I explained the Hill Cipher, in today’s module we will learn one more substitution cipher technique that is - Playfair Cipher.

Playfair cipher is a multi- alphabet letter encryption cipher, which deals with letters in plaintext as single units and renders these units into Ciphertext letters. The Playfair algorithm is based on the use of  a 5X5 matrix of letters built using a keyword.



5x5 Matrix

Now the question is how to fill that 5x5 matrix? - To fill that, we need a keyword or a message, after that you fill the letters of the keyword into the 5x5 matrix from left to right and from top to bottom and then fill the remainders of the matrix with the remaining letters in alphabetic order.

The letter I and J are treated as one letter and they are settled in the same box of the matrix like this - I/J. Plaintext is encrypted two letters at a time, so initially you have to pair the plaintext.

Rules to encrypt the message


Step1

Reoccurring plaintext letters that are in the same pair have to come apart with a filler such as “X”, let’s say Balloon is there for the message, so how are we going to proceed?

Matrix

Here you can see we paired the plaintext, and we also can see that repeated letters are paired with a filler, like a pair in “Balloon” - -> “LL” is replaced with filler -- > “X” à “LX” and “LO”.

Step 2

Two plaintext letters that drop in the same [ROW] in the matrix, have to be replaced with the letter to the right. Let’s say for example = “Balloon” is your plaintext.

Matrix

Now if you want to encrypt the pair of [X, O] - -> than it will be like this - > as [X, O] falls in the same row, then you have to replace the letter to the right, that is [O, B].

[X, O] - - > [O, B]

Step 3

The above step is for the Row Matrix, this step will show you the process of Column matrix. Let’s say two plaintext letters that drop in the same [Column] in the matrix have to be replaced with the letter beneath, with the last element of the column circularly follow the top element.

Matrix

The step shows how to encrypt a pair - > [B, U], the step says you have to replace the letter beneath the letter you want to encrypt, with the last element of the column circularly following the top element.

[B, U] - -> [N, B]

Step 4

Otherwise you just have to find the corners of the letter to encrypt, let’s you want to encrypt [H, S] letter, then just find out the corner, you get your encrypted letter.

Matrix

Example based on Playfair Cipher

Let’s say we want to encrypt the plaintext – “MONARCHY”.

Matrix

Now you have encrypt the pair [M, O] || [N, A] || [R, C] || [H, Y]. Apply all the steps of Playfair cipher that we discuss above, you get the cipher text as shown below:

Matrix

Implementation of Playfair Cipher

  1. #  
  2. include < stdio.h > #include < conio.h > #include < string.h > #include < ctype.h > #define MX 5  
  3. void playfair(char ch1, char ch2, char key[MX][MX]) {  
  4.     int i, j, w, x, y, z;  
  5.     FILE * out;  
  6.     if ((out = fopen("cipher.txt""a+")) == NULL) {  
  7.         printf("File Currupted.");  
  8.     }  
  9.     for (i = 0; i < MX; i++) {  
  10.         for (j = 0; j < MX; j++) {  
  11.             if (ch1 == key[i][j]) {  
  12.                 w = i;  
  13.                 x = j;  
  14.             } else if (ch2 == key[i][j]) {  
  15.                 y = i;  
  16.                 z = j;  
  17.             }  
  18.         }  
  19.     }  
  20.     //printf("%d%d %d%d",w,x,y,z);  
  21.     if (w == y) {  
  22.         x = (x + 1) % 5;  
  23.         z = (z + 1) % 5;  
  24.         printf("%c%c", key[w][x], key[y][z]);  
  25.         fprintf(out, "%c%c", key[w][x], key[y][z]);  
  26.     } else if (x == z) {  
  27.         w = (w + 1) % 5;  
  28.         y = (y + 1) % 5;  
  29.         printf("%c%c", key[w][x], key[y][z]);  
  30.         fprintf(out, "%c%c", key[w][x], key[y][z]);  
  31.     } else {  
  32.         printf("%c%c", key[w][z], key[y][x]);  
  33.         fprintf(out, "%c%c", key[w][z], key[y][x]);  
  34.     }  
  35.     fclose(out);  
  36. }  
  37. void main() {  
  38.     int i, j, k = 0, l, m = 0, n;  
  39.     char key[MX][MX], keyminus[25], keystr[10], str[25] = {  
  40.         0  
  41.     };  
  42.     char alpa[26] = {  
  43.         'A',  
  44.         'B',  
  45.         'C',  
  46.         'D',  
  47.         'E',  
  48.         'F',  
  49.         'G',  
  50.         'H',  
  51.         'I',  
  52.         'J',  
  53.         'K',  
  54.         'L',  
  55.         'M',  
  56.         'N',  
  57.         'O',  
  58.         'P',  
  59.         'Q',  
  60.         'R',  
  61.         'S',  
  62.         'T',  
  63.         'U',  
  64.         'V',  
  65.         'W',  
  66.         'X',  
  67.         'Y ',  
  68.         'Z'  
  69.     };  
  70.     printf("\nEnter key:");  
  71.     gets(keystr);  
  72.     printf("\nEnter the plain text:");  
  73.     gets(str);  
  74.     n = strlen(keystr);  
  75.     //convert the characters to uppertext  
  76.     for (i = 0; i < n; i++) {  
  77.         if (keystr[i] == 'j') keystr[i] = 'i';  
  78.         else if (keystr[i] == 'J') keystr[i] = 'I';  
  79.         keystr[i] = toupper(keystr[i]);  
  80.     }  
  81.     //convert all the characters of plaintext to uppertext  
  82.     for (i = 0; i < strlen(str); i++) {  
  83.         if (str[i] == 'j') str[i] = 'i';  
  84.         else if (str[i] == 'J') str[i] = 'I';  
  85.         str[i] = toupper(str[i]);  
  86.     }  
  87.     // store all characters except key  
  88.     j = 0;  
  89.     for (i = 0; i < 26; i++) {  
  90.         for (k = 0; k < n; k++) {  
  91.             if (keystr[k] == alpa[i]) break;  
  92.             else if (alpa[i] == 'J'break;  
  93.         }  
  94.         if (k == n) {  
  95.             keyminus[j] = alpa[i];  
  96.             j++;  
  97.         }  
  98.     }  
  99.     //construct key keymatrix  
  100.     k = 0;  
  101.     for (i = 0; i < MX; i++) {  
  102.         for (j = 0; j < MX; j++) {  
  103.             if (k < n) {  
  104.                 key[i][j] = keystr[k];  
  105.                 k++;  
  106.             } else {  
  107.                 key[i][j] = keyminus[m];  
  108.                 m++;  
  109.             }  
  110.             printf("%c ", key[i][j]);  
  111.         }  
  112.         printf("\n");  
  113.     }  
  114.     // construct diagram and convert to cipher text  
  115.     printf("\n\nEntered text :%s\nCipher Text :", str);  
  116.     for (i = 0; i < strlen(str); i++) {  
  117.         if (str[i] == 'J') str[i] = 'I';  
  118.         if (str[i + 1] == '\0') playfair(str[i], 'X', key);  
  119.         else {  
  120.             if (str[i + 1] == 'J') str[i + 1] = 'I';  
  121.             if (str[i] == str[i + 1]) playfair(str[i], 'X', key);  
  122.             else {  
  123.                 playfair(str[i], str[i + 1], key);  
  124.                 i++;  
  125.             }  
  126.         }  
  127.     }  
  128.     getch();  
  129. }  
Output window

Matrix

Hope you like it, Thank you for Reading, Have a good Day!


Similar Articles