-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRSA.java
More file actions
317 lines (245 loc) · 9.59 KB
/
RSA.java
File metadata and controls
317 lines (245 loc) · 9.59 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
package jmaxibus1184;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.nio.charset.*;
import java.util.*;
public class RSA extends JFrame implements ActionListener {
public static String chars = "";
public final int size = 4096;
final File public_key = new File("public_key");
final File private_key = new File("private_key");
final JButton generate = new JButton("generate");
final JButton store_pk = new JButton("store pk");
final JButton load_pk = new JButton("load pk");
final JButton share_pk = new JButton("share pk");
final JButton send = new JButton("send");
final JButton receive = new JButton("receive");
public BigInteger[] my_pk = new BigInteger[2];
public BigInteger[] kek_pk = new BigInteger[2];
public BigInteger[] sk = new BigInteger[2];
public RSA() {
this.setVisible(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(800, 500);
this.setBounds((1920 - this.getWidth()) / 2, (1080 - this.getHeight()) / 2, this.getWidth(), this.getHeight());
this.setLayout(new GridLayout(2, 3));
generate.addActionListener(this);
store_pk.addActionListener(this);
load_pk.addActionListener(this);
share_pk.addActionListener(this);
send.addActionListener(this);
receive.addActionListener(this);
this.add(generate);
this.add(store_pk);
this.add(load_pk);
this.add(share_pk);
this.add(send);
this.add(receive);
this.setVisible(true);
try {
if (!public_key.exists() || !private_key.exists()) {
switch (JOptionPane.showConfirmDialog(this, "NO KEY PAIR FOUND, GENERATE NEW ONE OR EXIT?", "?", JOptionPane.YES_NO_OPTION)) {
case JOptionPane.YES_OPTION:
generate();
JOptionPane.showMessageDialog(this, "NEW KEY PAIR GENERATED SUCCESSFULLY");
case JOptionPane.NO_OPTION:
System.exit(0);
}
}
String data = null;
Scanner myReader = new Scanner(public_key);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
}
myReader.close();
my_pk[0] = new BigInteger(data.split(",")[0]);
my_pk[1] = new BigInteger(data.split(",")[1]);
data = null;
myReader = new Scanner(private_key);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
}
myReader.close();
sk[0] = new BigInteger(data.split(",")[0]);
sk[1] = new BigInteger(data.split(",")[1]);
} catch (Exception scheisse) {
scheisse.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) {
for(int i=13312; i<40907; i++){
if(i != 21325 && i != 21328) {
chars += String.valueOf((char) i);
}
}
RSA rsa = new RSA();
}
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() == generate) {
if (JOptionPane.showConfirmDialog(this, "DO YOU REALLY WANNA GENERATE A NEW KEY PAIR?", "?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
generate();
JOptionPane.showMessageDialog(this, "NEW KEY PAIR GENERATED SUCCESSFULLY");
}
}
if (ev.getSource() == store_pk) {
try {
String data = Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor).toString();
String n = data.split(",")[0];
String e = data.split(",")[1];
String name = JOptionPane.showInputDialog(this, "NAME");
storePk(new BigInteger(n), new BigInteger(e), name);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
if (ev.getSource() == load_pk) {
String name = JOptionPane.showInputDialog(this, "name");
if(name.length() > 0) {
loadPk(name);
}
}
if (ev.getSource() == share_pk) {
String string = my_pk[0].toString() + "," + my_pk[1].toString();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(string), null);
}
if (ev.getSource() == send) {
if (kek_pk[0] == null) {
JOptionPane.showMessageDialog(this, "PLEASE LOAD KEK PK FIRST!", "ERROR", JOptionPane.ERROR_MESSAGE);
} else {
send();
}
}
if (ev.getSource() == receive) {
receive();
}
}
public void generate() {
BigInteger p;
BigInteger q;
BigInteger e;
Random rand = new Random();
p = BigInteger.probablePrime(size, rand);
q = BigInteger.probablePrime(size, rand);
e = BigInteger.probablePrime(size, rand);
BigInteger n = p.multiply(q);
BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
BigInteger d = e.modInverse(phi);
try {
FileWriter pk = new FileWriter(public_key);
pk.write(n.toString());
pk.write(",");
pk.write(e.toString());
pk.close();
FileWriter sk = new FileWriter(private_key);
sk.write(n.toString());
sk.write(",");
sk.write(d.toString());
sk.close();
} catch (Exception scheisse) {
scheisse.printStackTrace();
System.exit(1);
}
}
public void send() {
BigInteger n = kek_pk[0];
BigInteger e = kek_pk[1];
String message = JOptionPane.showInputDialog(null, "MESSAGE");
if (message.length() >= 7*(size/32)) {
JOptionPane.showMessageDialog(null,"DU KEK, DIE NACHRICHT IST ZU LANG!");
}else {
String encrypted = encrypt(n, e, message).toString();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(encrypted), null);
}
}
public void receive() {
BigInteger n = sk[0];
BigInteger d = sk[1];
String message_encrypted = null;
try {
message_encrypted = Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor).toString();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
String message = decrypt(n, d, message_encrypted);
JOptionPane.showMessageDialog(this, message, "DECRYPTED MESSAGE", JOptionPane.INFORMATION_MESSAGE);
}
public void storePk(BigInteger n, BigInteger e, String name) {
try {
FileWriter pk = new FileWriter(name);
pk.write(n.toString());
pk.write(",");
pk.write(e.toString());
pk.close();
} catch (Exception fuck) {
fuck.printStackTrace();
System.exit(1);
}
}
public void loadPk(String name) {
try {
String data = null;
File myObj = new File(name);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
}
myReader.close();
kek_pk[0] = new BigInteger(data.split(",")[0]);
kek_pk[1] = new BigInteger(data.split(",")[1]);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public String encrypt(BigInteger n, BigInteger e, String message) {
return encryptedToString(stringToBi(message).modPow(e, n));
}
public String decrypt(BigInteger n, BigInteger d, String encryptedMessage) {
return biToString(stringToEncrypted(encryptedMessage).modPow(d, n));
}
public BigInteger stringToBi(String message) {
byte[] bytes;
bytes = message.getBytes(StandardCharsets.UTF_8);
Random rd = new Random();
byte[] padding = new byte[128];
rd.nextBytes(padding);
byte[] result = Arrays.copyOf(bytes, bytes.length + padding.length);
System.arraycopy(padding, 0, result, bytes.length, padding.length);
return new BigInteger(result);
}
public String biToString(BigInteger bi) {
byte[] bytes;
bytes = bi.toByteArray();
byte[] result = new byte[bytes.length-128];
for (int i =0; i<bytes.length-128; i++){
result[i] = bytes[i];
}
return new String(result, StandardCharsets.UTF_8);
}
public String encryptedToString(BigInteger encrypted) {
String result = "";
while (!encrypted.equals(BigInteger.ZERO)) {
result = result + chars.charAt(encrypted.mod(BigInteger.valueOf(chars.length())).intValue());
encrypted = encrypted.divide(BigInteger.valueOf(chars.length()));
}
return result;
}
public BigInteger stringToEncrypted(String encrypted) {
BigInteger result = new BigInteger("0");
for (int index = encrypted.length()-1; index>=0;index--) {
result = result.multiply(BigInteger.valueOf(chars.length()));
result = result.add(BigInteger.valueOf(chars.indexOf(encrypted.charAt(index))));
}
return result;
}
}