-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.c
75 lines (71 loc) · 1.67 KB
/
rsa.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<math.h>
// Returns gcd of a and b
int gcd(int a, int h)
{
int temp;
while (1)
{
temp = a%h;
if (temp == 0)
return h;
a = h;
h = temp;
}
}
int modInverse(int c, int b)
{
for (int x = 1; x < b; x++)
if (((c%b) * (x%b)) % b == 1)
return x;
}
// Code to demonstrate RSA algorithm
int main()
{
// Two random prime numbers
double p = 7;
double q = 11;
// First part of public key:
double n = p*q;
// Finding other part of public key.
// e stands for encrypt
double e = 13;
double phi = (p-1)*(q-1);
printf("phi = %lf",phi);
while (e < phi)
{
// e must be co-prime to phi and
// smaller than phi.
if (gcd(e, phi)==1)
break;
else
e++;
}
// Private key (d stands for decrypt)
// choosing d such that it satisfies
// d*e = 1 + k * totient
// int k = 2; // A constant value
// double d = (1 + (k*phi))/e;
// double d = fmod(pow(e,-1),phi);
// printf("%lf",d);
double d;
d = modInverse(13,phi);
printf("\n d = %lf",d);
// Message to be encrypted
double msg = 5;
printf("\n Message data = %lf", msg);
// Encryption c = (msg ^ e) % n
double c = pow(msg, e);
c = fmod(c, n);
printf("\n Encrypted data = %lf", c);
// Decryption m = (c ^ d) % n
printf("\n c = %lf",c);
printf("\n d = %lf",d);
printf("\n n = %lf",n);
double m = pow(c, d);
printf("\n m = %lf",m);
double a;
a = fmod(m, n);
printf("\n Original Message Sent = %lf", a);
return 0;
}