-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa_algorithm.c
More file actions
60 lines (50 loc) · 1.52 KB
/
rsa_algorithm.c
File metadata and controls
60 lines (50 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <string.h>
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
long long mod_pow(long long base, long long exp, long long mod) {
long long res = 1;
base %= mod;
while (exp > 0) {
if (exp % 2) res = (res * base) % mod;
exp /= 2;
base = (base * base) % mod;
}
return res;
}
int mod_inverse(int e, int phi) {
int t = 0, newt = 1, r = phi, newr = e;
while (newr != 0) {
int q = r / newr, temp = newt;
newt = t - q * newt; t = temp;
temp = newr; newr = r - q * newr; r = temp;
}
return t < 0 ? t + phi : t;
}
int main() {
int p, q, e;
printf("Enter prime number p: "); scanf("%d", &p);
printf("Enter prime number q: "); scanf("%d", &q);
int n = p * q, phi = (p - 1) * (q - 1);
printf("Enter public exponent e (coprime with %d): ", phi); scanf("%d", &e);
while (gcd(e, phi) != 1) {
printf("e is not coprime with phi. Enter again: ");
scanf("%d", &e);
}
int d = mod_inverse(e, phi);
printf("\nPublic Key: (%d, %d)\nPrivate Key: (%d, %d)\n", e, n, d, n);
char msg[100]; printf("Enter message (plaintext): "); scanf("%s", msg);
int len = strlen(msg);
long long enc[100];
printf("\nEncrypted: ");
for (int i = 0; i < len; i++) {
enc[i] = mod_pow((int)msg[i], e, n);
printf("%lld ", enc[i]);
}
printf("\nDecrypted: ");
for (int i = 0; i < len; i++)
printf("%c", (char)mod_pow(enc[i], d, n));
printf("\n");
return 0;
}