This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.java
174 lines (153 loc) · 3.38 KB
/
RSA.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.math.BigInteger;
public class RSA {
private BigInteger p, q, n, phi, e, d;
/**
* ================================================================================
* Accessors and mutators
* ================================================================================
*/
public BigInteger p() {
return p;
}
public void p(BigInteger p) {
if (!p.equals(this.p)) {
if (this.p != null) {
throw new RuntimeException();
}
this.p = p;
p_q_n__p_q_n();
p_q_phi__p_q_phi();
}
}
public BigInteger q() {
return q;
}
public void q(BigInteger q) {
if (!q.equals(this.q)) {
if (this.q != null) {
throw new RuntimeException();
}
this.q = q;
p_q_n__p_q_n();
p_q_phi__p_q_phi();
}
}
/**
* @return Modulus: p * q
*/
public BigInteger n() {
return n;
}
/**
* @param n Modulus: p * q
*/
public void n(BigInteger n) {
if (!n.equals(this.n)) {
if (this.n != null) {
throw new RuntimeException();
}
this.n = n;
p_q_n__p_q_n();
}
}
/**
* @return (p - 1) * (q - 1)
*/
public BigInteger phi() {
return phi;
}
/**
* @param phi (p - 1) * (q - 1)
*/
public void phi(BigInteger phi) {
if (!phi.equals(this.phi)) {
if (this.phi != null) {
throw new RuntimeException();
}
this.phi = phi;
p_q_phi__p_q_phi();
e_phi__d();
}
}
/**
* @return Public exponent
*/
public BigInteger e() {
return e;
}
/**
* @param e Public exponent
*/
public void e(BigInteger e) {
if (!e.equals(this.e)) {
if (this.e != null) {
throw new RuntimeException();
}
this.e = e;
e_phi__d();
}
}
/**
* @return Private exponent
*/
public BigInteger d() {
return d;
}
/**
* @param d Private exponent
*/
public void d(BigInteger d) {
if (!d.equals(this.d)) {
if (this.d != null) {
throw new RuntimeException();
}
this.d = d;
}
}
/**
* ================================================================================
* Relations
* ================================================================================
*/
private void p_q_phi__p_q_phi() {
if (p != null && phi != null) {
// q = (p - 1) / phi + 1
q(phi.divide(p.subtract(BigInteger.ONE)).add(BigInteger.ONE));
} else if (p != null && q != null) {
// phi = (p - 1) * (q - 1)
phi(p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)));
} else if (phi != null && q != null) {
// p = (q - 1) / phi + 1
p(phi.divide(q.subtract(BigInteger.ONE)).add(BigInteger.ONE));
}
}
private void p_q_n__p_q_n() {
if (p != null && n != null) {
// q = n / p
q(n.divide(p));
} else if (p != null && q != null) {
// n = p * q
n(p.multiply(q));
} else if (n != null && q != null) {
// p = n / q
p(n.divide(q));
}
}
private void e_phi__d() {
if (e != null && phi != null) {
// d * e ≡ 1 (mod phi)
d(e.modInverse(phi));
}
}
/**
* ================================================================================
* Encryption and decryption
* ================================================================================
*/
public BigInteger encrypt(BigInteger pt) {
return pt.modPow(e, n);
}
public BigInteger decrypt(BigInteger ct) {
return ct.modPow(d, n);
}
}