-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.h
More file actions
43 lines (36 loc) · 1.36 KB
/
rsa.h
File metadata and controls
43 lines (36 loc) · 1.36 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
#include "utilities.h" // inverse_modulo, gcd, power_modulo, genPseudoRandomInt
class RSA {
private:
void init(unsigned long int p, unsigned long int q, unsigned long int e) {
this->p = p;
this->q = q;
this->n = p * q;
this->e = e;
this->d = inverse_modulo(e, (p-1)*(q-1));
}
public:
long unsigned int p, q; // two prime numbers
long unsigned int n; // product of two prime numbers p and q
long unsigned int e, d; // e*d = 1 (mod (p-1)*(q-1))
// p and q must be prime and e must be relatively prime with (p-1)*(q-1) and 1 < e < (p-1)*(q-1)
RSA(unsigned long int p, unsigned long int q, unsigned long int e) {
init(p, q, e);
}
// p and q must be prime
RSA(unsigned long int p, unsigned long int q) {
unsigned long int e;
while (true) {
e = genPseudoRandomInt(2, (p-1)*(q-1)-1);
if (gcd(e, (p-1)*(q-1)) == 1) {
break;
}
}
init(p, q, e);
}
unsigned long encodeMessage(unsigned long message) {
return power_modulo(message, this->e, this->n);
}
unsigned long decodeMessage(unsigned long message) {
return power_modulo(message, this->d, this->n);
}
};