IMohammedNasr

Untitled

May 16th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. // C program to implement Playfair Cipher
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define SIZE 30
  8.  
  9. // Function to convert the string to lowercase
  10. void toLowerCase(char plain[], int ps)
  11. {
  12.     int i;
  13.     for (i = 0; i < ps; i++)
  14.     {
  15.         if (plain[i] > 64 && plain[i] < 91)
  16.             plain[i] += 32;
  17.     }
  18. }
  19.  
  20. // Function to remove all spaces in a string
  21. int removeSpaces(char *plain, int ps)
  22. {
  23.     int i, count = 0;
  24.     for (i = 0; i < ps; i++)
  25.         if (plain[i] != ' ')
  26.             plain[count++] = plain[i];
  27.     plain[count] = '\0';
  28.     return count;
  29. }
  30.  
  31. // Function to generate the 5x5 key square
  32. void generateKeyTable(char key[], int ks, char keyT[5][5])
  33. {
  34.     int i, j, k, flag = 0, *dicty;
  35.  
  36.     // a 26 character hashmap
  37.     // to store count of the alphabet
  38.     dicty = (int *)calloc(26, sizeof(int));
  39.     for (i = 0; i < ks; i++)
  40.     {
  41.         if (key[i] != 'j')
  42.             dicty[key[i] - 97] = 2;
  43.     }
  44.  
  45.     dicty['j' - 97] = 1;
  46.  
  47.     i = 0;
  48.     j = 0;
  49.  
  50.     for (k = 0; k < ks; k++)
  51.     {
  52.         if (dicty[key[k] - 97] == 2)
  53.         {
  54.             dicty[key[k] - 97] -= 1;
  55.             keyT[i][j] = key[k];
  56.             j++;
  57.             if (j == 5)
  58.             {
  59.                 i++;
  60.                 j = 0;
  61.             }
  62.         }
  63.     }
  64.  
  65.     for (k = 0; k < 26; k++)
  66.     {
  67.         if (dicty[k] == 0)
  68.         {
  69.             keyT[i][j] = (char)(k + 97);
  70.             j++;
  71.             if (j == 5)
  72.             {
  73.                 i++;
  74.                 j = 0;
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. // Function to search for the characters of a digraph
  81. // in the key square and return their position
  82. void search(char keyT[5][5], char a, char b, int arr[])
  83. {
  84.     int i, j;
  85.  
  86.     if (a == 'j')
  87.         a = 'i';
  88.     else if (b == 'j')
  89.         b = 'i';
  90.  
  91.     for (i = 0; i < 5; i++)
  92.     {
  93.  
  94.         for (j = 0; j < 5; j++)
  95.         {
  96.  
  97.             if (keyT[i][j] == a)
  98.             {
  99.                 arr[0] = i;
  100.                 arr[1] = j;
  101.             }
  102.             else if (keyT[i][j] == b)
  103.             {
  104.                 arr[2] = i;
  105.                 arr[3] = j;
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. // Function to find the modulus with 5
  112. int mod5(int a) { return (a % 5); }
  113.  
  114. // Function to make the plain text length to be even
  115. int prepare(char str[], int ptrs)
  116. {
  117.     if (ptrs % 2 != 0)
  118.     {
  119.         str[ptrs++] = 'z';
  120.         str[ptrs] = '\0';
  121.     }
  122.     return ptrs;
  123. }
  124.  
  125. // Function for performing the encryption
  126. void encrypt(char str[], char keyT[5][5], int ps)
  127. {
  128.     int i, a[4];
  129.  
  130.     for (i = 0; i < ps; i += 2)
  131.     {
  132.  
  133.         search(keyT, str[i], str[i + 1], a);
  134.  
  135.         if (a[0] == a[2])
  136.         {
  137.             str[i] = keyT[a[0]][mod5(a[1] + 1)];
  138.             str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
  139.         }
  140.         else if (a[1] == a[3])
  141.         {
  142.             str[i] = keyT[mod5(a[0] + 1)][a[1]];
  143.             str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
  144.         }
  145.         else
  146.         {
  147.             str[i] = keyT[a[0]][a[3]];
  148.             str[i + 1] = keyT[a[2]][a[1]];
  149.         }
  150.     }
  151. }
  152.  
  153. // Function to encrypt using Playfair Cipher
  154. void encryptByPlayfairCipher(char str[], char key[])
  155. {
  156.     char ps, ks, keyT[5][5];
  157.  
  158.     // Key
  159.     ks = strlen(key);
  160.     ks = removeSpaces(key, ks);
  161.     toLowerCase(key, ks);
  162.  
  163.     // Plaintext
  164.     ps = strlen(str);
  165.     toLowerCase(str, ps);
  166.     ps = removeSpaces(str, ps);
  167.  
  168.     ps = prepare(str, ps);
  169.  
  170.     generateKeyTable(key, ks, keyT);
  171.  
  172.     encrypt(str, keyT, ps);
  173. }
  174.  
  175. // Driver code
  176. int main()
  177. {
  178.     char str[SIZE], key[SIZE];
  179.  
  180.     // Key to be encrypted
  181.     strcpy(key, "PLAYFAIR");
  182.     printf("Key text: %s\n", key);
  183.  
  184.     // Plaintext to be encrypted
  185.     strcpy(str, "instruments");
  186.     printf("Plain text: %s\n", str);
  187.  
  188.     // encrypt using Playfair Cipher
  189.     encryptByPlayfairCipher(str, key);
  190.  
  191.     printf("Cipher text: %s\n", str);
  192.  
  193.     return 0;
  194. }
  195.  
  196. // This code is contributed by AbhayBhat
  197.  
Advertisement
Add Comment
Please, Sign In to add comment