Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void initMatrix(char *matrix)
  6. {
  7. int index = 0;
  8.  
  9. for (int i = 48; i <= 57; i++) {
  10. matrix[index] = i;
  11. index++;
  12. }
  13.  
  14. for (int i = 65; i <= 90; i++) {
  15. matrix[index] = i;
  16. index++;
  17. }
  18.  
  19. for (int i = 97; i <= 122; i++) {
  20. matrix[index] = i;
  21. index++;
  22. }
  23.  
  24. matrix[62] = ' ';
  25. matrix[63] = ',';
  26. }
  27.  
  28. void mapMatrix(char *input, char *matrix, char *cypher) {
  29. int index = 0;
  30.  
  31. char * ptr;
  32. ptr = strtok (input," ");
  33.  
  34. while (ptr != NULL) {
  35. int n = atoi(ptr);
  36. cypher[index] = matrix[n];
  37. ptr = strtok (NULL, " ");
  38. index++;
  39. }
  40. }
  41.  
  42. void decrypt(char *hash, char *input, char *cypher) {
  43.  
  44. }
  45.  
  46. void encrypt(char *hash, char *input, char *cypher) {
  47. int offset = 0;
  48.  
  49. for (int i = 0; i < 256; i++) {
  50. if (input[i] == '\0')
  51. return;
  52.  
  53. for (int j = 0; j < 64; j++)
  54. if (hash[i % (strlen(hash) - 1)] == cypher[j]) {
  55. offset = j;
  56. break;
  57. }
  58.  
  59. for (int j = 0; j < 64; j++)
  60. if (input[i] == cypher[j]) {
  61. printf("%c", cypher[(j + offset) % (strlen(cypher) - 1)]);
  62. }
  63. }
  64. }
  65.  
  66. int main() {
  67. char matrix[64];
  68. char cypher[64];
  69.  
  70. char input[256];
  71. char hash[256];
  72. char operation[9];
  73.  
  74. initMatrix(matrix);
  75. fgets(operation, sizeof(operation), stdin);
  76.  
  77. fgets(input, sizeof(input), stdin);
  78.  
  79. mapMatrix(input, matrix, cypher);
  80.  
  81. fgets(hash, sizeof(hash), stdin);
  82. fgets(input, sizeof(input), stdin);
  83.  
  84. if (!(strcmp(operation, "encrypt\n")))
  85. encrypt(hash, input, cypher);
  86.  
  87. else if (!(strcmp(operation, "decrypt\n")))
  88. decrypt(hash, input, cypher);
  89.  
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement