Advertisement
Guest User

Untitled

a guest
May 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5.  
  6. // #include </home/ref0il/Desktop/RSA/myBigNum.h>
  7.  
  8. #define MAX 1000000
  9.  
  10. int findTheNearestSimple(int n)
  11. {
  12.     int *a = (int*)malloc(n * sizeof(int)),
  13.         i,
  14.         j,
  15.         k;
  16.     memset(a, 0, sizeof(int) * n);
  17.     for(i = 1; 3 * i + 1 < n; i++)
  18.         for(j = 1; (k = i + j + 2 * i * j) < n && j <= i; j++)
  19.             a[k] = 1;
  20.     for(i = n - 1; i >= 1; i--)
  21.         if (a[i] == 0)
  22.             return (2 * i + 1);
  23.     free(a);
  24. }
  25.  
  26. int gcd(int a, int b)
  27. {
  28.     while (b)
  29.     {
  30.         int c = a % b;
  31.         a = b;
  32.         b = c;
  33.     }
  34.     return abs(a);
  35. }
  36.  
  37. int main(const int const argc, const char * const argv[])
  38. {
  39.    
  40.     setlocale(LC_ALL, "Rus");
  41.  
  42.     if (argc == 1)
  43.     {
  44.         int p, q;
  45.         while (p == q)
  46.         {
  47.             printf("Ввелите число p: ");
  48.             scanf("%i", &p);
  49.             printf("\nВведите число q: ");
  50.             scanf("%i", &q);
  51.             printf("\n");
  52.             if (p == q)
  53.                 printf("\nВы ввели 2 одинаковых числа. Введите новое значение для однoго из них.\n\n");
  54.         }
  55.        
  56.         printf("\nPlease, wait. Key generation in process...\n");
  57.         int p_simple = findTheNearestSimple(p),
  58.         q_simple = findTheNearestSimple(q),
  59.         d,
  60.         d_simple = 0;
  61.  
  62.         unsigned int n = p_simple*q_simple;
  63.  
  64.         while (d_simple != 1)
  65.         {
  66.             d = rand()%100;
  67.             d_simple = gcd(d, ((p_simple-1)*(q_simple-1)));
  68.         }
  69.  
  70.         unsigned int e = 0, e_simple = 0;
  71.         while (e_simple != 1)
  72.         {
  73.             e += 1;
  74.             e_simple = (e*d)%((p_simple-1)*(q_simple-1));
  75.         }
  76.         printf("{%12u,%12u} - Open key\n", e, n);
  77.         printf("{%12i,%12u} - Secret key\n\n", d, n);
  78.  
  79.         char *Text = malloc(MAX);
  80.         printf("Your message is taken from <input.txt>:\n");
  81.         FILE *fin = fopen("input.txt", "r");
  82.         for (int i = 0; i < 2; i++)
  83.             fgets(Text, MAX, fin);
  84.         printf("%s\n", Text);
  85.  
  86.         unsigned long long *CryptoText = (unsigned long long*)malloc(MAX * sizeof(unsigned long long));
  87.  
  88.         printf("\n%-5s%6s%20s%14s%16s\n-------------------------------------------------------------\n",
  89.             "Text", "ASCII", "CryptoText/Block#", "ASCIIdecrypt", "Text decrypt");
  90.  
  91.         for (long long j = 0, b = 301; j < strlen(Text); j++, b++)
  92.         {
  93.             long long c = 1;
  94.             unsigned long long i = 0;
  95.             long long ASCIIcode = (long long)Text[j] + b;
  96.             while (i < e)
  97.             {
  98.                 c *= ASCIIcode;
  99.                 c %= n;
  100.                 i++;
  101.             }
  102.             CryptoText[j] = c;
  103.         }
  104.  
  105.         FILE *fout = fopen("output.txt", "w");
  106.         fprintf(fout, "%u %u %u\n", e, d, n);
  107.  
  108.         for (long long j = 0; j < strlen(Text); j++)
  109.         {
  110.             fprintf(fout, "%lli ", CryptoText[j]);
  111.             printf("%-4c|%6lli|%18llu|%14s|%14s|\n", Text[j], (long long)Text[j], CryptoText[j], "Secret", "Secret");
  112.         }
  113.  
  114.         printf("\n\nВы закодировали предложение: %s. \nКод вашего зашифрованного предложения хранится в:\noutput.txt", Text);
  115.     }
  116.  
  117.     if (argc > 1)
  118.     {
  119.         FILE *fin = fopen("output.txt", "r");
  120.         FILE *fout = fopen("output1.txt", "w");
  121.         unsigned int e,
  122.                      d,
  123.                      n;
  124.        
  125.         fscanf(fin, "%u %u %u", &e, &d, &n);
  126.    
  127.         unsigned int *Hash = (unsigned int*)malloc(sizeof(unsigned int) * MAX);
  128.         int count = 0;
  129.         while (fscanf(fin, "%i", &Hash[count]) != EOF)
  130.             count++;
  131.  
  132.         unsigned int *Tdecrypt = (unsigned int*)malloc(sizeof(unsigned int) * MAX);
  133.         for (unsigned int j = 0, b = 301; j < MAX; j++, b++)
  134.         {
  135.             unsigned int m = 1;
  136.             unsigned int i = 0;
  137.             while (i < d)
  138.             {
  139.                 m *= Hash[j];
  140.                 m %= n;
  141.                 i++;
  142.             }
  143.             m -= b;
  144.             Tdecrypt[j] = m;
  145.         }
  146.  
  147.         free(Hash);
  148.         for (int i = 0; i < count; i++)
  149.             fprintf(fout, "%c", (char)Tdecrypt[i]);
  150.        
  151.         printf("\nРасшифрованное сообщение было помещено в output1.txt\n\n");
  152.  
  153.         fclose(fin);
  154.         fclose(fout);
  155.     }
  156.  
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement