fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 30
  6.  
  7. // Function to convert the 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. // Function to remove all spaces in a string
  18. int removeSpaces(char* plain, int ps)
  19. {
  20. int i, count = 0;
  21. for (i = 0; i < ps; i++)
  22. if (plain[i] != ' ')
  23. plain[count++] = plain[i];
  24. plain[count] = '\0';
  25. return count;
  26. }
  27.  
  28. // Function to generate the 5x5 key square
  29. void generateKeyTable(char key[], int ks, char keyT[5][5])
  30. {
  31. int i, j, k, flag = 0, *dicty;
  32.  
  33. // a 26 character hashmap
  34. // to store count of the alphabet
  35. dicty = (int*)calloc(26, sizeof(int));
  36. for (i = 0; i < ks; i++) {
  37. if (key[i] != 'j')
  38. dicty[key[i] - 97] = 2;
  39. }
  40.  
  41. dicty['j' - 97] = 1;
  42.  
  43. i = 0;
  44. j = 0;
  45.  
  46. for (k = 0; k < ks; k++) {
  47. if (dicty[key[k] - 97] == 2) {
  48. dicty[key[k] - 97] -= 1;
  49. keyT[i][j] = key[k];
  50. j++;
  51. if (j == 5) {
  52. i++;
  53. j = 0;
  54. }
  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. // Function to 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, char b, int arr[])
  73. {
  74. int i, j;
  75.  
  76. if (a == 'j')
  77. a = 'i';
  78. else if (b == 'j')
  79. b = 'i';
  80.  
  81. for (i = 0; i < 5; i++) {
  82.  
  83. for (j = 0; j < 5; j++) {
  84.  
  85. if (keyT[i][j] == a) {
  86. arr[0] = i;
  87. arr[1] = j;
  88. }
  89. else if (keyT[i][j] == b) {
  90. arr[2] = i;
  91. arr[3] = j;
  92. }
  93. }
  94. }
  95. }
  96.  
  97. // Function to find the modulus with 5
  98. int mod5(int a)
  99. {
  100. return (a % 5);
  101. }
  102.  
  103. // Function to make the plain text length to be even
  104. int prepare(char str[], int ptrs)
  105. {
  106. if (ptrs % 2 != 0) {
  107. str[ptrs++] = 'z';
  108. str[ptrs] = '\0';
  109. }
  110. return ptrs;
  111. }
  112.  
  113. // Function for performing the encryption
  114. void encrypt(char str[], char keyT[5][5], int ps)
  115. {
  116. int i, a[4];
  117.  
  118. for (i = 0; i < ps; i += 2) {
  119.  
  120. search(keyT, str[i], str[i + 1], a);
  121.  
  122. if (a[0] == a[2]) {
  123. str[i] = keyT[a[0]][mod5(a[1] + 1)];
  124. str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
  125. }
  126. else if (a[1] == a[3]) {
  127. str[i] = keyT[mod5(a[0] + 1)][a[1]];
  128. str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
  129. }
  130. else {
  131. str[i] = keyT[a[0]][a[3]];
  132. str[i + 1] = keyT[a[2]][a[1]];
  133. }
  134. }
  135. }
  136.  
  137. // Function to encrypt using Playfair Cipher
  138. void encryptByPlayfairCipher(char str[], char key[])
  139. {
  140. char ps, ks, keyT[5][5];
  141.  
  142. // Key
  143. ks = strlen(key);
  144. ks = removeSpaces(key, ks);
  145. toLowerCase(key, ks);
  146.  
  147. // Plaintext
  148. ps = strlen(str);
  149. toLowerCase(str, ps);
  150. ps = removeSpaces(str, ps);
  151.  
  152. ps = prepare(str, ps);
  153.  
  154. generateKeyTable(key, ks, keyT);
  155.  
  156. encrypt(str, keyT, ps);
  157. }
  158.  
  159. // Driver code
  160. int main()
  161. {
  162. char str[SIZE], key[SIZE];
  163.  
  164. // Key to be encrypted
  165. strcpy(key, "Monarchy");
  166. printf("Key text: %s\n", key);
  167.  
  168. // Plaintext to be encrypted
  169. strcpy(str, "instruments");
  170. printf("Plain text: %s\n", str);
  171.  
  172. // encrypt using Playfair Cipher
  173. encryptByPlayfairCipher(str, key);
  174.  
  175. printf("Cipher text: %s\n", str);
  176.  
  177. return 0;
  178. }
Success #stdin #stdout 0s 4488KB
stdin
Standard input is empty
stdout
Key text: Monarchy
Plain text: instruments
Cipher text: gatlmzclrqtx