C Program to Encrypt Message using Playfair Cipher

This is a C Program to implement Playfair cipher. The Playfair cipher is a digraph substitution cipher. It employs a table where one letter of the alphabet is omitted, and the letters are arranged in a 5×5 grid. Typically, the J is removed from the alphabet and an I takes its place in the text that is to be encoded.

To encode a message, one breaks it into two-letter chunks. Repeated letters in the same chunk are usually separated by an X. The message, “HELLO ONE AND ALL” would become “HE LX LO ON EA ND AL LX”. Since there was not an even number of letters in the message, it was padded with a spare X. Next, you take your letter pairs and look at their positions in the grid.

“HE” forms two corners of a rectangle. The other letters in the rectangle are C and K. You start with the H and slide over to underneath the E and write down K. Similarly, you take the E and slide over to the H column to get C. So, the first two letters are “KC”. “LX” becomes “NV” in the same way.

Here is the source code of the C Program to Encode a Message Using Playfair Cipher. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2.  
  3. int check(char table[5][5], char k) {
  4.     int i, j;
  5.     for (i = 0; i < 5; ++i)
  6.         for (j = 0; j < 5; ++j) {
  7.             if (table[i][j] == k)
  8.                 return 0;
  9.         }
  10.     return 1;
  11. }
  12.  
  13. void main() {
  14.     int i, j, key_len;
  15.     char table[5][5];
  16.     for (i = 0; i < 5; ++i)
  17.         for (j = 0; j < 5; ++j)
  18.             table[i][j] = '0';
  19.  
  20.     printf("**********Playfair Cipher************\n\n");
  21.  
  22.     printf("Enter the length of the Key. ");
  23.     scanf("%d", &key_len);
  24.  
  25.     char key[key_len];
  26.  
  27.     printf("Enter the Key. ");
  28.     for (i = -1; i < key_len; ++i) {
  29.         scanf("%c", &key[i]);
  30.         if (key[i] == 'j')
  31.             key[i] = 'i';
  32.     }
  33.  
  34.     int flag;
  35.     int count = 0;
  36.  
  37.     // inserting the key into the table
  38.     for (i = 0; i < 5; ++i) {
  39.         for (j = 0; j < 5; ++j) {
  40.             flag = 0;
  41.             while (flag != 1) {
  42.                 if (count > key_len)
  43.                     goto l1;
  44.  
  45.                 flag = check(table, key[count]);
  46.                 ++count;
  47.             }// end of while
  48.             table[i][j] = key[(count - 1)];
  49.         }// end of inner for
  50.     }// end of outer for
  51.  
  52.  
  53.     l1: printf("\n");
  54.  
  55.     int val = 97;
  56.     //inserting other alphabets
  57.     for (i = 0; i < 5; ++i) {
  58.         for (j = 0; j < 5; ++j) {
  59.             if (table[i][j] >= 97 && table[i][j] <= 123) {
  60.             } else {
  61.                 flag = 0;
  62.                 while (flag != 1) {
  63.                     if ('j' == (char) val)
  64.                         ++val;
  65.                     flag = check(table, (char) val);
  66.                     ++val;
  67.                 }// end of while
  68.                 table[i][j] = (char) (val - 1);
  69.             }//end of else
  70.         }// end of inner for
  71.     }// end of outer for
  72.  
  73.     printf("The table is as follows:\n");
  74.     for (i = 0; i < 5; ++i) {
  75.         for (j = 0; j < 5; ++j) {
  76.             printf("%c ", table[i][j]);
  77.         }
  78.         printf("\n");
  79.     }
  80.  
  81.     int l = 0;
  82.     printf("\nEnter the length length of plain text.(without spaces) ");
  83.     scanf("%d", &l);
  84.  
  85.     printf("\nEnter the Plain text. ");
  86.     char p[l];
  87.     for (i = -1; i < l; ++i) {
  88.         scanf("%c", &p[i]);
  89.     }
  90.  
  91.     for (i = -1; i < l; ++i) {
  92.         if (p[i] == 'j')
  93.             p[i] = 'i';
  94.     }
  95.  
  96.     printf("\nThe replaced text(j with i)");
  97.     for (i = -1; i < l; ++i)
  98.         printf("%c ", p[i]);
  99.  
  100.     count = 0;
  101.     for (i = -1; i < l; ++i) {
  102.         if (p[i] == p[i + 1])
  103.             count = count + 1;
  104.     }
  105.  
  106.     printf("\nThe cipher has to enter %d bogus char.It is either 'x' or 'z'\n",
  107.             count);
  108.  
  109.     int length = 0;
  110.     if ((l + count) % 2 != 0)
  111.         length = (l + count + 1);
  112.     else
  113.         length = (l + count);
  114.  
  115.     printf("\nValue of length is %d.\n", length);
  116.     char p1[length];
  117.  
  118.     //inserting bogus characters.
  119.     char temp1;
  120.     int count1 = 0;
  121.     for (i = -1; i < l; ++i) {
  122.         p1[count1] = p[i];
  123.         if (p[i] == p[i + 1]) {
  124.             count1 = count1 + 1;
  125.             if (p[i] == 'x')
  126.                 p1[count1] = 'z';
  127.             else
  128.                 p1[count1] = 'x';
  129.         }
  130.         count1 = count1 + 1;
  131.     }
  132.  
  133.     //checking for length
  134.  
  135.     char bogus;
  136.     if ((l + count) % 2 != 0) {
  137.         if (p1[length - 1] == 'x')
  138.             p1[length] = 'z';
  139.         else
  140.             p1[length] = 'x';
  141.     }
  142.  
  143.     printf("The final text is:");
  144.     for (i = 0; i <= length; ++i)
  145.         printf("%c ", p1[i]);
  146.  
  147.     char cipher_text[length];
  148.     int r1, r2, c1, c2;
  149.     int k1;
  150.  
  151.     for (k1 = 1; k1 <= length; ++k1) {
  152.         for (i = 0; i < 5; ++i) {
  153.             for (j = 0; j < 5; ++j) {
  154.                 if (table[i][j] == p1[k1]) {
  155.                     r1 = i;
  156.                     c1 = j;
  157.                 } else if (table[i][j] == p1[k1 + 1]) {
  158.                     r2 = i;
  159.                     c2 = j;
  160.                 }
  161.             }//end of for with j
  162.         }//end of for with i
  163.  
  164.         if (r1 == r2) {
  165.             cipher_text[k1] = table[r1][(c1 + 1) % 5];
  166.             cipher_text[k1 + 1] = table[r1][(c2 + 1) % 5];
  167.         }
  168.  
  169.         else if (c1 == c2) {
  170.             cipher_text[k1] = table[(r1 + 1) % 5][c1];
  171.             cipher_text[k1 + 1] = table[(r2 + 1) % 5][c1];
  172.         } else {
  173.             cipher_text[k1] = table[r1][c2];
  174.             cipher_text[k1 + 1] = table[r2][c1];
  175.         }
  176.  
  177.         k1 = k1 + 1;
  178.     }//end of for with k1
  179.  
  180.     printf("\n\nThe Cipher text is:\n ");
  181.     for (i = 1; i <= length; ++i)
  182.         printf("%c ", cipher_text[i]);
  183.  
  184. }

Output:

advertisement
advertisement
$ gcc PlayfairEncryption.c
$ ./a.out
 
**********Playfair Cipher************
 
Enter the length of the Key. 15
Enter the Key. playfairexample
 
The table is as follows:
p l a y f 
i r e x m 
b c d g h 
k n o q s 
t u v w z 
 
Enter the length of plain text. (without spaces) 25
 
Enter the Plain text. hidethegoldinthetreestump
 
The replaced text(j with i)
 h i d e t h e g o l d i n t h e t r e e s t u m p 
 
The cipher has to enter 1 bogus char. It is either 'x' or 'z'
 
Value of length is 26.
The final text is:
 h i d e t h e g o l d i n t h e t r e x e s t u m p 
 
The Cipher text is:
 b m o d z b x d n a b e k u d m u i x m m o u v i f

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.