-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.java
More file actions
321 lines (291 loc) · 9.98 KB
/
Copy pathRSA.java
File metadata and controls
321 lines (291 loc) · 9.98 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Networked Chat with RSA Encryption/Decryption
* Project 5
* CS 342 - Fall 2017
*
* - Margi Katwala
* - Bushra Baqui
* - Aditya Sinha
*
* **RSA.java**
*/
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
public class RSA
{
// mods the integers by a certain power
public static LargeInteger modPower(final LargeInteger a, final LargeInteger p, final LargeInteger mod)
{
LargeInteger returnNumber = new LargeInteger(1);
LargeInteger valN = new LargeInteger(a);
LargeInteger publicPrivate = new LargeInteger(p);
while (LargeInteger.compare(publicPrivate, new LargeInteger(0)) > 0)
{
if (!publicPrivate.isEven())
{
returnNumber.multiply(valN);
if (returnNumber.toString().length() > 50 && LargeInteger.compare(returnNumber, mod) >= 0) returnNumber = returnNumber.mod(mod);
}
publicPrivate = LargeInteger.divideBy2(publicPrivate);
valN.multiply(valN);
if (valN.toString().length() > 50 && LargeInteger.compare(valN, mod) >= 0)
valN = valN.mod(mod);
}
return returnNumber.mod(mod);
}
// takes the inverse of the integer for deblocking...
public static LargeInteger inverse(LargeInteger a, LargeInteger modNumber)
{
LargeInteger numberType = new LargeInteger(0);
LargeInteger numberTypeLarge = new LargeInteger(1);
LargeInteger remainderNumber = new LargeInteger(modNumber);
LargeInteger remainderNumberLarge = new LargeInteger(a).mod(modNumber);
if (LargeInteger.compare(modNumber, new LargeInteger(0)) < 0)
{
modNumber.setNegative(!modNumber.isNegativeNumber());
}
if (LargeInteger.compare(a, new LargeInteger(0)) < 0)
{
LargeInteger newNumber = new LargeInteger(a);
newNumber.setNegative(true);
LargeInteger tmp = new LargeInteger(modNumber);
tmp.subtract(newNumber.mod(modNumber));
modNumber = tmp;
}
while (LargeInteger.compare(remainderNumberLarge, new LargeInteger(0)) != 0)
{
LargeInteger quotientNumber = new LargeInteger(remainderNumber).divide(remainderNumberLarge);
LargeInteger largeNumber = new LargeInteger(numberTypeLarge);
numberTypeLarge = new LargeInteger(numberType).subtract(new LargeInteger(quotientNumber).multiply(numberTypeLarge));
numberType = new LargeInteger(largeNumber);
largeNumber = new LargeInteger(remainderNumberLarge);
remainderNumberLarge = new LargeInteger(remainderNumber).subtract(new LargeInteger(quotientNumber).multiply(remainderNumberLarge));
remainderNumber = new LargeInteger(largeNumber);
}
if (LargeInteger.compare(remainderNumber, new LargeInteger(1)) > 0)
{
return new LargeInteger(-1);
}
if (LargeInteger.compare(numberType, new LargeInteger(0)) < 0)
{
numberType.add(modNumber);
}
return numberType;
}
// gets the random prime number from a file...
public static LargeInteger random_prime(int bits)
{
while (true)
{
LargeInteger r = randomNumber(bits);
BigInteger bigInteger = new BigInteger(r.toString());
if (bigInteger.isProbablePrime(10))
{
return r;
}
}
}
public static Key getlocalKey()
{
LargeInteger x = new LargeInteger(37);
LargeInteger xx = new LargeInteger(39);
return generateKeys(x,xx);
}
// gets the random integer
public static LargeInteger randomNumber(int bits)
{
Random random = new Random();
LargeInteger ret = new LargeInteger(0);
for (int i = 0; i < bits; i++)
{
if (random.nextBoolean())
{
ret.add(new LargeInteger(2).power(i));
}
}
return ret;
}
// function to generate the public and private keys
public static Key generateKeys(LargeInteger p, LargeInteger q)
{
LargeInteger
n = new LargeInteger(p).multiply(q),
t = new LargeInteger(new LargeInteger(p).subtract(new LargeInteger(1))).multiply(new LargeInteger(q).subtract(new LargeInteger(1)));
LargeInteger d;
LargeInteger e;
while (true)
{
while (true)
{
e = random_prime(5);
if (LargeInteger.compare(new LargeInteger(t).mod(e), new LargeInteger(0)) == 0)
continue;
if (LargeInteger.compare(e, t) < 0)
break;
}
d = inverse(e, t);
if (LargeInteger.compare(d, new LargeInteger(-1)) != 0)
break;
}
return new Key(e, d, n);
}
static ArrayList<LargeInteger> encrypt(String message, LargeInteger n, LargeInteger e, int block) {
ArrayList<LargeInteger> unencrypted = blockString(message, block);
ArrayList<LargeInteger> encrypted = new ArrayList<>();
for (LargeInteger integer : unencrypted) {
encrypted.add(encrypt(integer, n, e));
}
return encrypted;
}
// blocks the message string that is being sent...
public static ArrayList<LargeInteger> blockString(String message, int block)
{
String currentLength = "";
ArrayList<LargeInteger> values = new ArrayList<>();
for (int i = 1; i <= message.length(); i++)
{
currentLength += message.charAt(i - 1);
if ((i) % block == 0) {
LargeInteger unencrypted = convertToInt(currentLength);
values.add(unencrypted);
currentLength = "";
}
}
if (currentLength.length() > 0)
{
while (currentLength.length() < block)
{
currentLength += (char) 0;
}
LargeInteger unencrypted = convertToInt(currentLength);
values.add(unencrypted);
}
return values;
}
// decrypts the string...
static String decrypt(ArrayList<LargeInteger> values, LargeInteger n, LargeInteger d, int block)
{
ArrayList<LargeInteger> unencrypted = new ArrayList<>();
for (LargeInteger value : values)
{
unencrypted.add(decrypt(value, n, d));
}
return unblock(unencrypted, block);
}
static String unblock(ArrayList<LargeInteger> values, int block)
{
String message = "";
for (LargeInteger value : values)
{
String curr = "";
for (int i = 1; i <= block; i++)
{
char c = (char) value.mod(new LargeInteger(100)).toInt();
value.divide(new LargeInteger(100));
if (c != 0) {
curr += (char) getDecoding(c);
}
}
message += curr;
}
return message;
}
// gets the encoding for the characters...
private static int getEncoding(char encodeCharacter)
{
if(encodeCharacter==0)
return 0;
else if(encodeCharacter==11)
return 1;
else if(encodeCharacter==9)
return 2;
else if(encodeCharacter==10)
return 3;
else if(encodeCharacter==13)
return 4;
else if(encodeCharacter==' ')
return 5;
else return encodeCharacter-27;
}
// gets the decoding for the characters...
private static int getDecoding(char decodeCharacter)
{
if(decodeCharacter==0)
return 0;
else if(decodeCharacter==1)
return 11;
else if(decodeCharacter==2)
return 9;
else if(decodeCharacter==3)
return 10;
else if(decodeCharacter==4)
return 13;
else if(decodeCharacter==5)
return ' ';
else return decodeCharacter+27;
}
// converts the string to an integer...
public static LargeInteger convertToInt(String m)
{
LargeInteger t = new LargeInteger();
for (int i = 0; i < m.length(); i++)
{
t.add(new LargeInteger(getEncoding(m.charAt(i))).multiply(new LargeInteger(100).power(i)));
}
return t;
}
// encrypts using mod
public static LargeInteger encrypt(LargeInteger m, LargeInteger n, LargeInteger e)
{
return modPower(m, e, n);
}
// decrypts using mod
public static LargeInteger decrypt(LargeInteger m, LargeInteger n, LargeInteger d)
{
return modPower(m, d, n);
}
// reads the file
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
public static class Key
{
public LargeInteger e, d, n;
public Key(LargeInteger e, LargeInteger d, LargeInteger n)
{
this.e = e;
this.d = d;
this.n = n;
}
@Override
public String toString()
{
return "Key{" +
"e=" + e +
", d=" + d +
", n=" + n +
'}';
}
}
}