Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- // #include </home/ref0il/Desktop/RSA/myBigNum.h>
- #define MAX 1000000
- int findTheNearestSimple(int n)
- {
- int *a = (int*)malloc(n * sizeof(int)),
- i,
- j,
- k;
- memset(a, 0, sizeof(int) * n);
- for(i = 1; 3 * i + 1 < n; i++)
- for(j = 1; (k = i + j + 2 * i * j) < n && j <= i; j++)
- a[k] = 1;
- for(i = n - 1; i >= 1; i--)
- if (a[i] == 0)
- return (2 * i + 1);
- free(a);
- }
- int gcd(int a, int b)
- {
- while (b)
- {
- int c = a % b;
- a = b;
- b = c;
- }
- return abs(a);
- }
- int main(const int const argc, const char * const argv[])
- {
- setlocale(LC_ALL, "Rus");
- if (argc == 1)
- {
- int p, q;
- while (p == q)
- {
- printf("Ввелите число p: ");
- scanf("%i", &p);
- printf("\nВведите число q: ");
- scanf("%i", &q);
- printf("\n");
- if (p == q)
- printf("\nВы ввели 2 одинаковых числа. Введите новое значение для однoго из них.\n\n");
- }
- printf("\nPlease, wait. Key generation in process...\n");
- int p_simple = findTheNearestSimple(p),
- q_simple = findTheNearestSimple(q),
- d,
- d_simple = 0;
- unsigned int n = p_simple*q_simple;
- while (d_simple != 1)
- {
- d = rand()%100;
- d_simple = gcd(d, ((p_simple-1)*(q_simple-1)));
- }
- unsigned int e = 0, e_simple = 0;
- while (e_simple != 1)
- {
- e += 1;
- e_simple = (e*d)%((p_simple-1)*(q_simple-1));
- }
- printf("{%12u,%12u} - Open key\n", e, n);
- printf("{%12i,%12u} - Secret key\n\n", d, n);
- char *Text = malloc(MAX);
- printf("Your message is taken from <input.txt>:\n");
- FILE *fin = fopen("input.txt", "r");
- for (int i = 0; i < 2; i++)
- fgets(Text, MAX, fin);
- printf("%s\n", Text);
- unsigned long long *CryptoText = (unsigned long long*)malloc(MAX * sizeof(unsigned long long));
- printf("\n%-5s%6s%20s%14s%16s\n-------------------------------------------------------------\n",
- "Text", "ASCII", "CryptoText/Block#", "ASCIIdecrypt", "Text decrypt");
- for (long long j = 0, b = 301; j < strlen(Text); j++, b++)
- {
- long long c = 1;
- unsigned long long i = 0;
- long long ASCIIcode = (long long)Text[j] + b;
- while (i < e)
- {
- c *= ASCIIcode;
- c %= n;
- i++;
- }
- CryptoText[j] = c;
- }
- FILE *fout = fopen("output.txt", "w");
- fprintf(fout, "%u %u %u\n", e, d, n);
- for (long long j = 0; j < strlen(Text); j++)
- {
- fprintf(fout, "%lli ", CryptoText[j]);
- printf("%-4c|%6lli|%18llu|%14s|%14s|\n", Text[j], (long long)Text[j], CryptoText[j], "Secret", "Secret");
- }
- printf("\n\nВы закодировали предложение: %s. \nКод вашего зашифрованного предложения хранится в:\noutput.txt", Text);
- }
- if (argc > 1)
- {
- FILE *fin = fopen("output.txt", "r");
- FILE *fout = fopen("output1.txt", "w");
- unsigned int e,
- d,
- n;
- fscanf(fin, "%u %u %u", &e, &d, &n);
- unsigned int *Hash = (unsigned int*)malloc(sizeof(unsigned int) * MAX);
- int count = 0;
- while (fscanf(fin, "%i", &Hash[count]) != EOF)
- count++;
- unsigned int *Tdecrypt = (unsigned int*)malloc(sizeof(unsigned int) * MAX);
- for (unsigned int j = 0, b = 301; j < MAX; j++, b++)
- {
- unsigned int m = 1;
- unsigned int i = 0;
- while (i < d)
- {
- m *= Hash[j];
- m %= n;
- i++;
- }
- m -= b;
- Tdecrypt[j] = m;
- }
- free(Hash);
- for (int i = 0; i < count; i++)
- fprintf(fout, "%c", (char)Tdecrypt[i]);
- printf("\nРасшифрованное сообщение было помещено в output1.txt\n\n");
- fclose(fin);
- fclose(fout);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement