1 package testrsa;
2
3
4
5 import java.math.BigInteger;
6 import java.security.KeyPair;
7 import java.security.KeyPairGenerator;
8 import java.security.NoSuchAlgorithmException;
9 import java.util.Random;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 @author
25 @version
26
27 public class RSA {
28
29
30
31
32 private BigInteger pubExp = new BigInteger("0");
33
34
35
36
37 private BigInteger pubMod = new BigInteger("0");
38
39
40
41
42 private BigInteger privKey = new BigInteger("0");
43
44
45
46
47 private int keyLength = 0;
48
49
50
51
52
53 private static final BigInteger one = new BigInteger("1");
54
55
56
57
58
59
60 private static final int certainty = 50;
61
62
63
64
65 public RSA() {
66 this.pubExp = new BigInteger("0");
67 this.pubMod = new BigInteger("0");
68 this.privKey = new BigInteger("0");
69 }
70
71
72
73
74
75 @param
76 @param
77
78 public RSA(BigInteger publicE, BigInteger publicM) {
79 this.pubExp = publicE;
80 this.pubMod = publicM;
81 this.privKey = new BigInteger("0");
82 }
83
84
85
86
87 @param
88 @param
89 @param
90
91 public RSA(BigInteger publicE, BigInteger publicM, BigInteger privateK) {
92 this.pubExp = publicE;
93 this.pubMod = publicM;
94 this.privKey = privateK;
95 }
96
97
98
99
100
101
102 @param
103 @return
104
105 public BigInteger encrypt(BigInteger msg) throws RsaException {
106 BigInteger cipher;
107 if (this.pubMod.compareTo(msg) == 1) {
108 cipher = msg.modPow(this.pubExp, this.pubMod);
109 } else {
110 throw new RsaException("Public modulus must be greater than the message!");
111 }
112 return cipher;
113 }
114
115
116
117
118 @param
119 @return
120
121 public BigInteger sign(BigInteger msg) throws RsaException {
122 if (this.privKey.equals(new BigInteger("0"))) {
123 throw new RsaException("can't sign a message without private key.");
124 }
125 BigInteger signat;
126 signat = msg.modPow(this.privKey, this.pubMod);
127 return signat;
128 }
129
130
131
132
133
134
135 @param
136 @return
137
138 public BigInteger verify(BigInteger signat) {
139 BigInteger veri;
140 veri = signat.modPow(this.pubExp, this.pubMod);
141 return veri;
142 }
143
144
145
146
147
148
149
150
151 @param
152 @return
153
154 public BigInteger decrypt(BigInteger cipher) throws RsaException {
155 if (this.privKey.equals(new BigInteger("0"))) {
156 throw new RsaException("no decryption without private key.");
157 }
158 BigInteger msg;
159 msg = cipher.modPow(this.privKey, this.pubMod);
160 return msg;
161 }
162
163
164
165
166
167
168
169
170 @param
171
172
173 @return
174
175 public int generateKeys(int keyL) {
176 BigInteger one = new BigInteger("1");
177 Random rand = new Random();
178 boolean found = false;
179 int keyLength = keyL;
180 if (keyL <= 0) {
181 keyLength = rand.nextInt(49);
182 keyLength = 512 + (keyLength * 32);
183 }
184 this.keyLength = keyLength;
185
186 BigInteger p = new BigInteger("0");
187 BigInteger q = new BigInteger("0");
188 BigInteger p1 = new BigInteger("0");
189 BigInteger q1 = new BigInteger("0");
190 BigInteger phi = new BigInteger("0");
191 BigInteger e = new BigInteger("0");
192
193
194 p = new BigInteger(keyLength/2, certainty, new Random());
195 q = new BigInteger(keyLength/2+1, certainty, new Random());
196
197 do {
198 e = new BigInteger(keyLength, certainty, new Random());
199 p1 = p.subtract(one);
200 q1 = q.subtract(one);
201 phi = p1.multiply(q1);
202 if ( phi.gcd(e).equals(one) ) {
203 found = true;
204 }
205 } while (!found);
206
207 BigInteger pubMod = p.multiply(q);
208 BigInteger pubExp = e;
209 BigInteger privateK = pubExp.modInverse(phi);
210
211 this.privKey = privateK;
212 this.pubExp = pubExp;
213 this.pubMod = pubMod;
214
215
216 return this.keyLength;
217 }
218
219
220
221
222
223
224
225 public int getKeyLength() {
226 return this.keyLength;
227 }
228
229
230
231
232
233
234 public BigInteger[] getPublicKey() {
235 BigInteger[] pubKey = new BigInteger[] {this.pubExp, this.pubMod};
236 return pubKey;
237 }
238
239
240
241
242
243
244 public BigInteger getPrivateKey() {
245
246 return this.privKey;
247 }
248
249
250 }
251