c rsa encryption

Solutions on MaxInterview for c rsa encryption by the best coders in the world

showing results for - "c rsa encryption"
Linus
10 Oct 2018
1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4#include <math.h>
5 
6int e, d, n;
7 
8int gcd(int a, int b)
9{
10    int q, r1, r2, r;
11 
12    if (a > b) {
13        r1 = a;
14        r2 = b;
15    }
16    else {
17        r1 = b;
18        r2 = a;
19    }
20 
21    while (r2 > 0) {
22        q = r1 / r2;
23        r = r1 - q * r2;
24        r1 = r2;
25        r2 = r;
26    }
27 
28    return r1;
29}
30 
31int PrimarityTest(int a, int i)
32{
33    int n = i - 1;
34    int k = 0;
35    int j, m, T;
36 
37    while (n % 2 == 0) {
38        k++;
39        n = n / 2;
40    }
41 
42    m = n;
43    T = FindT(a, m, i);
44 
45    if (T == 1 || T == i - 1)
46        return 1;
47 
48    for (j = 0; j < k; j++) {
49        T = FindT(T, 2, i);
50        if (T == 1)
51            return 0;
52        if (T == i - 1)
53            return 1;
54    }
55    return 0;
56}
57 
58int FindT(int a, int m, int n)
59{
60    int r;
61    int y = 1;
62 
63    while (m > 0) {
64        r = m % 2;
65        FastExponention(r, n, &y, &a);
66        m = m / 2;
67    }
68    return y;
69}
70 
71int FastExponention(int bit, int n, int* y, int* a)
72{
73    if (bit == 1)
74        *y = (*y * (*a)) % n;
75    
76    *a = (*a) * (*a) % n;
77}
78 
79int inverse(int a, int b)
80{
81    int inv;
82    int q, r, r1 = a, r2 = b, t, t1 = 0, t2 = 1;
83 
84    while (r2 > 0) {
85        q = r1 / r2;
86        r = r1 - q * r2;
87        r1 = r2;
88        r2 = r;
89 
90        t = t1 - q * t2;
91        t1 = t2;
92        t2 = t;
93    }
94 
95    if (r1 == 1)
96        inv = t1;
97 
98    if (inv < 0)
99        inv = inv + a;
100 
101    return inv;
102}
103 
104int KeyGeneration()
105{
106    int p, q;
107    int phi_n;
108    
109    do {
110        do
111            p = rand();
112        while (p % 2 == 0);
113 
114    } while (!PrimarityTest(2, p));
115 
116    do {
117        do
118            q = rand();
119        while (q % 2 == 0);
120    } while (!PrimarityTest(2, q));
121 
122    n = p * q;
123    phi_n = (p - 1) * (q - 1);
124 
125    do
126        e = rand() % (phi_n - 2) + 2; // 1 < e < phi_n
127    while (gcd(e, phi_n) != 1);
128 
129    d = inverse(phi_n, e);
130}
131 
132int Encryption(int value, FILE* out)
133{
134    int cipher;
135    cipher = FindT(value, e, n);
136    fprintf(out, "%d ", cipher);
137}
138 
139int Decryption(int value, FILE* out)
140{
141    int decipher;
142    decipher = FindT(value, d, n);
143    fprintf(out, "%c", decipher);
144}
145 
146int main(void)
147{
148    FILE *inp, *out;
149 
150    // destroy contents of these files (from previous runs, if any)
151    out = fopen("cipher.txt", "w+");
152    fclose(out);
153    out = fopen("decipher.txt", "w+");
154    fclose(out);
155 
156    KeyGeneration();
157 
158    inp = fopen("plain.txt", "r+");
159    if (inp == NULL) {
160        printf("Error opening Source File.\n");
161        exit(1);
162    }
163 
164    out = fopen("cipher.txt", "w+");
165    if (out == NULL) {
166        printf("Error opening Destination File.\n");
167        exit(1);
168    }
169 
170    // encryption starts
171    while (1) {
172        char ch = getc(inp);
173        if (ch == -1)
174            break;
175        int value = toascii(ch);
176        Encryption(value, out);
177    }
178 
179    fclose(inp);
180    fclose(out);
181 
182    // decryption starts
183    inp = fopen("cipher.txt", "r");
184    if (inp == NULL) {
185        printf("Error opening Cipher Text.\n");
186        exit(1);
187    }
188 
189    out = fopen("decipher.txt", "w+");
190    if (out == NULL) {
191        printf("Error opening File.\n");
192        exit(1);
193    }
194 
195    while (1) {
196        int cip;
197        if (fscanf(inp, "%d", &cip) == -1)
198            break;
199        Decryption(cip, out);
200    }
201    fclose(out);
202 
203    return 0;
204}