Beyrin

RC4

Mar 26th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 256   // 2^8
  5.  
  6.   void swap(unsigned char *a, unsigned char *b) //Карра Кееро Карра Кееро
  7. {
  8.     int tmp = *a;
  9.     *a = *b;
  10.     *b = tmp;
  11. }
  12.  
  13. int KSA(char *key, unsigned char *S) //Да, да, курсач тоже будешь в ночь делать?
  14. {
  15.     int len = strlen(key);
  16.     int j = 0;
  17.     for(int i = 0; i < N; i++)
  18.         S[i] = i;
  19.     for(int i = 0; i < N; i++)
  20.     {
  21.         j = (j + S[i] + key[i % len]) % N;
  22.         swap(&S[i], &S[j]);
  23.     }
  24.     return 0;
  25. }
  26. int PRGA(unsigned char *S, char *plaintext, unsigned char *ciphertext) //Ну хули ты начинаешь, нормально же общались
  27. {
  28.     int i = 0;
  29.     int j = 0;
  30.     for(size_t n = 0, len = strlen(plaintext); n < len; n++)
  31.     {
  32.         i = (i + 1) % N;
  33.         j = (j + S[i]) % N;
  34.         swap(&S[i], &S[j]);
  35.         int rnd = S[(S[i] + S[j]) % N];
  36.         ciphertext[n] = rnd ^ plaintext[n];
  37.     }
  38.     return 0;
  39. }
  40. int RC4(char *key, char *plaintext, unsigned char *ciphertext) //Вот скажи мне, Олег, какого хуя ты такой распиздяй?
  41. {
  42.     unsigned char S[N];
  43.     KSA(key, S);
  44.     PRGA(S, plaintext, ciphertext);
  45.     return 0;
  46. }
  47. int main(int argc, char *argv[])
  48. {
  49.     //  кроучи хуй знает зачем ара 6 утра помоги мне как же я з а е б а л с я
  50.     char str[256];
  51.     FILE *LeFinale = fopen("output.txt", "w+a");
  52.     char kostil[] = "-m";
  53.     if (argc == 3 )
  54.     {
  55.         unsigned char *ciphertext = malloc(sizeof(int) * strlen(argv[2]));
  56.         // printf("One %s\n", argv[1]);
  57.         // printf("Two %s\n", argv[2]);
  58.         if (strcmp(argv[1], kostil) == 0)
  59.         {
  60.             RC4(argv[1], argv[2], ciphertext);
  61.             for(size_t i = 0, len = strlen(argv[2]); i < len; i++)
  62.             {
  63.                 putc(ciphertext[i], LeFinale);
  64.             }
  65.             fclose(LeFinale);
  66.             //printf("%02hhX", ciphertext[i]);
  67.         }
  68.         else
  69.         {
  70.            // printf("%s", argv[1]);
  71.             printf("ERROR! Wrong argument\n");
  72.         }
  73.     }
  74.     else
  75.     {
  76.         printf("No arguments has been found.\nEnter text.\n");
  77.         scanf( "%s", str);
  78.         unsigned char *ciphertext = malloc(sizeof(int) * strlen(str));
  79.         argv[1] = "-m";// ДА ЭТО ЕБУЧИЙ КОСТЫЛЬ НЕ СМОТРИ
  80.         argv[2] = str;
  81.         RC4(argv[1], argv[2], ciphertext);
  82.         for(size_t i = 0, len = strlen(argv[2]); i < len; i++)
  83.         {
  84.             putc(ciphertext[i], LeFinale);
  85.         }
  86.         fputc('\n', LeFinale);
  87.         for(size_t i = 0, len = strlen(argv[2]); i < len; i++)
  88.         {
  89.             putc(str[i], LeFinale);
  90.         }
  91.         putc("\n", LeFinale);
  92.         RC4(argv[1], ciphertext, ciphertext);
  93.         for(size_t i = 0, len = strlen(argv[2]); i < len; i++)
  94.         {
  95.             putc(ciphertext[i], LeFinale);
  96.         }
  97.         //fprintf(LeFinale, str);
  98.         for(size_t i = 0, len = strlen(argv[2]); i < len; i++)
  99.         {
  100.             putc(str[i], LeFinale);
  101.         }
  102.         fclose(LeFinale);
  103.         printf("%s", str);
  104.     }
  105.     return 0;
  106. }
Add Comment
Please, Sign In to add comment