-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.cpp
158 lines (100 loc) · 2.33 KB
/
RSA.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
SHIV's RSA Algorithm
*/
#include<iostream>
#include<math.h>
#include<cstdlib>
int check_prime(unsigned long int n);
unsigned long int encrypt(unsigned long int, unsigned long int, unsigned long int);
unsigned long int decrypt(unsigned long int, unsigned long int, unsigned long int);
unsigned long int jpl(unsigned long int, unsigned long int, unsigned long int);
int main()
{
double r;
int a;
unsigned long int p, q, n, phin, e, d, k;
unsigned long int c, m, mm;
// generate 2 prime numbers < 100
a = 0;
// while(a==0) {
// r = ((double) rand() / (RAND_MAX));
// p = int(r*100.0);
// a = check_prime(p);
// }
// a = 0;
// while(a==0) {
// r = ((double) rand() / (RAND_MAX));
// q = int(r*100.0);
// a = check_prime(q);
// }
std::cout << "Enter p: " ;
std::cin >> p;
std::cout << "Enter q: " ;
std::cin >> q;
std::cout<<"p,q = "<<p<<" "<<q<<"\n";
n = p*q;
phin = (p-1)*(q-1);
// generate e such that 1<e<phin, e and phin are coprime
a = 0;
while(a==0) {
r = ((double) rand() / (RAND_MAX));
e = int(0.1*r*double(phin));
a = check_prime(e);
}
std::cout<<"public key, n, e: "<<n<<" "<<e<<" \n";
// generate private key, d
k = 5;
while((1+k*phin)%e!=0){
k++;
}
d = (1 + k*phin)/e;
std::cout<<" k, d "<<k<<" "<<d<<" \n";
// secret message to be encrypted
std::cout<<"\n enter secret message to be encrypted: \n";
std::cin>>m;
// encrypt message
c = encrypt(n,e,m);
std::cout<<"encrypted message: "<<c<<" \n";
// decrypt message
mm = decrypt(n,d,c);
std::cout<<"decrypted message "<<mm<<" \n";
return 0;
}
// check for prime: return 1 if prime; 0 otherwise
int check_prime(unsigned long int n)
{
unsigned long int i;
for(i = 2; i<= sqrt(n); i++)
{
if(n%i == 0){
return 0;
}
}
return 1;
}
// encrypt
unsigned long int encrypt(unsigned long int n, unsigned long int e, unsigned long int m)
{
unsigned long int c, i;
c = jpl(m,e,n);
return c;
}
// decrypt
unsigned long int decrypt(unsigned long int n, unsigned long int d, unsigned long int c)
{
unsigned long int m;
m = jpl(c,d,n);
return m;
}
// (b^e)%m
unsigned long int jpl(unsigned long int b, unsigned long int e, unsigned long int m)
{
unsigned long int c, e1;
c = 1;
e1 = 0;
while(e1<e){
e1++;
c = (b*c)%m;
}
return c;
}