-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrm_RSA_AES.java
More file actions
373 lines (344 loc) · 19.1 KB
/
frm_RSA_AES.java
File metadata and controls
373 lines (344 loc) · 19.1 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package week03;
import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.logging. Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author levan
*/
public class frm_RSA_AES extends javax.swing.JFrame {
/**
* Creates new form frm_RSA_AES
*/
private RSA_AES_Cipher rsacipher;
public frm_RSA_AES() {
initComponents();
try {
rsacipher = new RSA_AES_Cipher();
} catch (Exception ex) {
Logger.getLogger(frm_RSA_AES.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
txt_privatekey = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
txt_plaintext = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
txt_ciphertext = new javax.swing.JTextArea();
btnGenerate = new javax.swing.JButton();
btnLoadkey = new javax.swing.JButton();
btnencrypt = new javax.swing.JButton();
btndeencrypt = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
txt_publickey = new javax.swing.JTextArea();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel4.setText("private key");
txt_privatekey.setColumns(20);
txt_privatekey.setRows(5);
jScrollPane3.setViewportView(txt_privatekey);
jLabel5.setText("ciphertext");
txt_plaintext.setColumns(20);
txt_plaintext.setRows(5);
jScrollPane1.setViewportView(txt_plaintext);
txt_ciphertext.setColumns(20);
txt_ciphertext.setRows(5);
jScrollPane4.setViewportView(txt_ciphertext);
btnGenerate.setText("Generate Keys");
btnGenerate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGenerateActionPerformed(evt);
}
});
btnLoadkey.setText("Load Keys");
btnLoadkey.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoadkeyActionPerformed(evt);
}
});
btnencrypt.setText("encrypt");
btnencrypt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnencryptActionPerformed(evt);
}
});
btndeencrypt.setText("dencrypt");
btndeencrypt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btndeencryptActionPerformed(evt);
}
});
jLabel1.setText("RSA-AES Cipher Demo");
jLabel2.setText("plaintext");
txt_publickey.setColumns(20);
txt_publickey.setRows(5);
jScrollPane2.setViewportView(txt_publickey);
jLabel3.setText("publickey");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(btnGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnLoadkey, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(42, 42, 42)
.addComponent(btnencrypt, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(47, 47, 47)
.addComponent(btndeencrypt, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1)
.addComponent(jScrollPane3)))))
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(84, 84, 84)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jLabel5))
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnGenerate)
.addComponent(btnLoadkey)
.addComponent(btnencrypt)
.addComponent(btndeencrypt)))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btnLoadkeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadkeyActionPerformed
// TODO add your handling code here:
try {
// Load public key from file
JFileChooser publicKeyChooser = new JFileChooser();
publicKeyChooser.setDialogTitle("Load Public Key File");
int publicKeyChooserResult = publicKeyChooser.showOpenDialog(this);
if (publicKeyChooserResult == JFileChooser.APPROVE_OPTION) {
File publicKeyFile = publicKeyChooser.getSelectedFile();
String publicKeyContent = readFile (publicKeyFile);
txt_publickey.setText(publicKeyContent);
}
// Load private key from file
JFileChooser privateKeyChooser = new JFileChooser();
privateKeyChooser.setDialogTitle ("Load Private Key File");
int privateKeyChooserResult = privateKeyChooser.showOpenDialog(this);
if (privateKeyChooserResult == JFileChooser.APPROVE_OPTION) {
File privateKeyFile = privateKeyChooser.getSelectedFile();
String privateKeyContent = readFile (privateKeyFile);
txt_privatekey.setText(privateKeyContent);
}
} catch (HeadlessException | IOException ex) {
Logger.getLogger(frm_RSA_AES.class.getName()).log (Level. SEVERE, null, ex);
JOptionPane.showMessageDialog(this,
"Error loading keys:" + ex.getMessage(),
"Error", JOptionPane. ERROR_MESSAGE);
}
}//GEN-LAST:event_btnLoadkeyActionPerformed
private String readFile (File file )throws IOException{
StringBuilder contentBuilder = new StringBuilder();
try(BufferedReader br = new BufferedReader(new FileReader(file))){
String line;
while((line = br.readLine())!= null) {
contentBuilder.append(line).append("\n");
}
}
return contentBuilder.toString();
}
private void btnGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateActionPerformed
// TODO add your handling code here:
try {
rsacipher = new RSA_AES_Cipher();
txt_publickey.setText(rsacipher.getPublicKey().toString());
txt_privatekey.setText(rsacipher.getPrivateKey().toString());
// Save public key to file
JFileChooser publicKeyChooser = new JFileChooser();
publicKeyChooser.setDialogTitle("Save Public Key File");
int publicKeyChooserResult = publicKeyChooser.showSaveDialog(this);
if (publicKeyChooserResult == JFileChooser.APPROVE_OPTION) {
File publicKeyFile = publicKeyChooser.getSelectedFile();
try (FileWriter writer = new FileWriter (publicKeyFile.getAbsolutePath())) {
writer.write(rsacipher.getPublicKey().toString());
JOptionPane.showMessageDialog(this,
"Public Key saved to file successfully.",
"Success", JOptionPane. INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error saving Public Key file: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}}
JFileChooser privateKeyChooser = new JFileChooser();
privateKeyChooser.setDialogTitle ("Save Private Key File");
int privateKeyChooserResult = privateKeyChooser.showSaveDialog(this);
if (privateKeyChooserResult == JFileChooser.APPROVE_OPTION) {
File privateKeyFile= privateKeyChooser.getSelectedFile();
try (FileWriter writer = new FileWriter (privateKeyFile.getAbsolutePath())) {
writer.write (rsacipher.getPrivateKey().toString());
JOptionPane.showMessageDialog(this,
"Private Key saved to file successfully.",
"Success", JOptionPane. INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error saving Private Key file: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}} catch (Exception ex) {
Logger.getLogger(frm_RSA_AES.class.getName()).log (Level. SEVERE, null, ex);
JOptionPane.showMessageDialog(this,
"Error generating RSA key pair: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_btnGenerateActionPerformed
private void btnencryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnencryptActionPerformed
// TODO add your handling code here
try {
String plaintext = txt_plaintext.getText();
byte[] encryptedBytes =rsacipher.encrypt(plaintext);
String encryptedText = new String (encryptedBytes, StandardCharsets. ISO_8859_1);
txt_ciphertext.setText(encryptedText);
} catch (IllegalBlockSizeException | BadPaddingException ex) {
Logger.getLogger(frm_RSA_AES.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Error encrypting: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception ex) {
Logger.getLogger(frm_RSA_AES.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Error encrypting:"+ ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_btnencryptActionPerformed
private void btndeencryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndeencryptActionPerformed
// TODO add your handling code here:
try {
byte[] combined = txt_ciphertext.getText().getBytes(StandardCharsets.ISO_8859_1);
String decryptedText = rsacipher.decrypt (combined);
txt_plaintext.setText(decryptedText);
} catch (IllegalBlockSizeException | BadPaddingException ex) {
Logger.getLogger(frm_RSA_AES.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Error decrypting: "+ ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception ex) {
Logger.getLogger(frm_RSA_AES.class.getName()).log (Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Error decrypting: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_btndeencryptActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frm_RSA_AES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frm_RSA_AES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frm_RSA_AES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frm_RSA_AES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frm_RSA_AES().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnGenerate;
private javax.swing.JButton btnLoadkey;
private javax.swing.JButton btndeencrypt;
private javax.swing.JButton btnencrypt;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea txt_ciphertext;
private javax.swing.JTextArea txt_plaintext;
private javax.swing.JTextArea txt_privatekey;
private javax.swing.JTextArea txt_publickey;
// End of variables declaration//GEN-END:variables
}