Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. #include "functions.h"
  6.  
  7. #define ALPHABET_LENGTH 26
  8. static char ReverseAlphabet[ALPHABET_LENGTH] = {
  9. 'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'
  10. };
  11.  
  12. static char Alphabet[ALPHABET_LENGTH] = {
  13. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
  14. };
  15.  
  16.  
  17. char *remove_duplicates(char *table)
  18. {
  19. char flag[208] = {0};
  20. int i, j = 0;
  21.  
  22. for (i = 0; table[i] != '\0'; i++) {
  23. if (0 == flag[table[i]]) {
  24. flag[table[i]] = 1;
  25. table[j] = table[i];
  26. j++;
  27. }
  28. }
  29. table[j] = '\0';
  30. return NULL;
  31. }
  32.  
  33. int function_encrypt(char *key, char *inFileData, char *outFileName, int size)
  34. {
  35. FILE *file = fopen(outFileName, "w");
  36.  
  37. int maxlen = (int)strlen(key) + ALPHABET_LENGTH;
  38. char out_data[size + 1];
  39. char *substitutionTable = (char*)malloc(maxlen);
  40. if (substitutionTable) {
  41.  
  42. // create the following:
  43. // 'K','E','Y','Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'
  44. strcpy(substitutionTable, key);
  45. strcat(substitutionTable, ReverseAlphabet);
  46.  
  47. remove_duplicates(substitutionTable);
  48.  
  49. for (int i = 0 ; i < strlen(inFileData); i++) {
  50. out_data[i] = substitutionTable[inFileData[i] - 'A'];
  51. }
  52.  
  53. out_data[size] = '\0';
  54. free(substitutionTable);
  55.  
  56. int val = fputs(out_data,file);
  57. if (val >= 0 ) {
  58. printf("\nSuccess");
  59. fclose(file);
  60. }
  61. else {
  62. printf("\nFailed");
  63. }
  64. return 0;
  65. }
  66. return -1; // return error
  67. }
  68.  
  69. int function_decrypt(char *key, char *inFileData, char *outFileName, int size)
  70. {
  71. FILE *file = fopen(outFileName, "w");
  72.  
  73. int maxlen = (int)strlen(key) + ALPHABET_LENGTH;
  74. char out_data[size + 1];
  75. char *substitutionTable = (char*)malloc(maxlen);
  76. if (substitutionTable) {
  77.  
  78. // create the following:
  79. // 'K','E','Y','Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'
  80. strcpy(substitutionTable, key);
  81. strcat(substitutionTable, ReverseAlphabet);
  82.  
  83. remove_duplicates(substitutionTable);
  84.  
  85. for (int i = 0 ; i < strlen(inFileData); i++) {
  86. const char *ptr = strchr(substitutionTable, inFileData[i]);
  87. if(ptr) {
  88. int index = ptr - substitutionTable;
  89. out_data[i] = Alphabet[index];
  90. }
  91. }
  92.  
  93. out_data[size] = '\0';
  94. free(substitutionTable);
  95.  
  96. int val = fputs(out_data,file);
  97. if (val >= 0 ) {
  98. printf("\nSuccess");
  99. fclose(file);
  100. }
  101. else {
  102. printf("\nFailed");
  103. }
  104. return 0;
  105. }
  106. return -1; // return error
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement