fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 30
  5.  
  6. // Convert all the characters
  7. // of a string to lowercase
  8. void toLowerCase(char plain[], int ps)
  9. {
  10. int i;
  11. for (i = 0; i < ps; i++) {
  12. if (plain[i] > 64 && plain[i] < 91)
  13. plain[i] += 32;
  14. }
  15. }
  16.  
  17. // Remove all spaces in a string
  18. // can be extended to remove punctuation
  19. int removeSpaces(char* plain, int ps)
  20. {
  21. int i, count = 0;
  22. for (i = 0; i < ps; i++)
  23. if (plain[i] != ' ')
  24. plain[count++] = plain[i];
  25. plain[count] = '\0';
  26. return count;
  27. }
  28.  
  29. // generates the 5x5 key square
  30. void generateKeyTable(char key[], int ks,
  31. char keyT[5][5])
  32. {
  33. int i, j, k, flag = 0, *dicty;
  34.  
  35. // a 26 character hashmap
  36. // to store count of the alphabet
  37. dicty = (int*)calloc(26, sizeof(int));
  38.  
  39. for (i = 0; i < ks; i++) {
  40. if (key[i] != 'j')
  41. dicty[key[i] - 97] = 2;
  42. }
  43. dicty['j' - 97] = 1;
  44.  
  45. i = 0;
  46. j = 0;
  47. for (k = 0; k < ks; k++) {
  48. if (dicty[key[k] - 97] == 2) {
  49. dicty[key[k] - 97] -= 1;
  50. keyT[i][j] = key[k];
  51. j++;
  52. if (j == 5) {
  53. i++;
  54. j = 0;
  55. }
  56. }
  57. }
  58. for (k = 0; k < 26; k++) {
  59. if (dicty[k] == 0) {
  60. keyT[i][j] = (char)(k + 97);
  61. j++;
  62. if (j == 5) {
  63. i++;
  64. j = 0;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. // Search for the characters of a digraph
  71. // in the key square and return their position
  72. void search(char keyT[5][5], char a,
  73. char b, int arr[])
  74. {
  75. int i, j;
  76.  
  77. if (a == 'j')
  78. a = 'i';
  79. else if (b == 'j')
  80. b = 'i';
  81.  
  82. for (i = 0; i < 5; i++) {
  83. for (j = 0; j < 5; j++) {
  84. if (keyT[i][j] == a) {
  85. arr[0] = i;
  86. arr[1] = j;
  87. }
  88. else if (keyT[i][j] == b) {
  89. arr[2] = i;
  90. arr[3] = j;
  91. }
  92. }
  93. }
  94. }
  95.  
  96. // Function to find the modulus with 5
  97. int mod5(int a)
  98. {
  99. return (a % 5);
  100. }
  101.  
  102. // Function to decrypt
  103. void decrypt(char str[], char keyT[5][5], int ps)
  104. {
  105. int i, a[4];
  106. for (i = 0; i < ps; i += 2) {
  107. search(keyT, str[i], str[i + 1], a);
  108. if (a[0] == a[2]) {
  109. str[i] = keyT[a[0]][mod5(a[1] - 1)];
  110. str[i + 1] = keyT[a[0]][mod5(a[3] - 1)];
  111. }
  112. else if (a[1] == a[3]) {
  113. str[i] = keyT[mod5(a[0] - 1)][a[1]];
  114. str[i + 1] = keyT[mod5(a[2] - 1)][a[1]];
  115. }
  116. else {
  117. str[i] = keyT[a[0]][a[3]];
  118. str[i + 1] = keyT[a[2]][a[1]];
  119. }
  120. }
  121. }
  122.  
  123. // Function to call decrypt
  124. void decryptByPlayfairCipher(char str[], char key[])
  125. {
  126. char ps, ks, keyT[5][5];
  127.  
  128. // Key
  129. ks = strlen(key);
  130. ks = removeSpaces(key, ks);
  131. toLowerCase(key, ks);
  132.  
  133. // ciphertext
  134. ps = strlen(str);
  135. toLowerCase(str, ps);
  136. ps = removeSpaces(str, ps);
  137.  
  138. generateKeyTable(key, ks, keyT);
  139.  
  140. decrypt(str, keyT, ps);
  141. }
  142.  
  143. // Driver code
  144. int main()
  145. {
  146. char str[SIZE], key[SIZE];
  147.  
  148. // Key to be encrypted
  149. strcpy(key, "cheater");
  150. printf("Key text: %s\n", key);
  151.  
  152. // Ciphertext to be decrypted
  153. strcpy(str, "XENPZTVNTX");
  154. printf("Plain text: %s\n", str);
  155.  
  156. // encrypt using Playfair Cipher
  157. decryptByPlayfairCipher(str, key);
  158.  
  159. printf("Deciphered text: %s\n", str);
  160.  
  161. return 0;
  162. }
  163.  
  164. // This code is contributed by AbhayBhat
  165.  
Success #stdin #stdout 0s 5432KB
stdin
Standard input is empty
stdout
Key text: cheater
Plain text: XENPZTVNTX
Deciphered text: qUkuu