-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathView.java
380 lines (329 loc) · 9.1 KB
/
View.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
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
373
374
375
376
377
378
379
380
package ecc.gui;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
//import ecc.elliptic.*;
//import ecc.*;
//import ecc.io.*;
public class View extends JFrame implements ActionListener
{
public final JButton ENCRYPT = new JButton("Encrypt");
public final JButton DECRYPT = new JButton("Decrypt");
// public final JButton GENKEY = new JButton("Gen. key");
public final JButton LOADKEY = new JButton("Load key");
public final JButton SAVEKEY = new JButton("Display key");
public final JButton QUIT = new JButton("Quit");
public final String TITLE = "JECC";
//File chosen;
public String Path;
private JScrollPane scroll_pane;
private JLabel infoLabel = new JLabel("Welcome to JECC ");
private JLabel statusLabel = new JLabel();
private JEditorPane pane;
private CryptoSystem cs;
private File targetFile;
private Key pk;
private Key sk;
public View(int width, int height, CryptoSystem cs)
{
try{
System.out.print("Loading " + TITLE +"...");
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
pane = new JEditorPane();
pane.setEditable(false);
pane.setFont(new Font("TimesNewRoman", Font.BOLD, 12));
String text="Welcome to the encryption phase. Here the text from the file displayed in the last step will be encrypted, using the standard key for ECC( "+cs+" ). You can view the secret and public keys used by clicking the Display Key button, or load a set of keys from a file.";
pane.setText(text);
scroll_pane = new JScrollPane(pane);
cp.add(scroll_pane, BorderLayout.CENTER);
JPanel top = new JPanel(new FlowLayout());
top.add(ENCRYPT); ENCRYPT.addActionListener(this);
top.add(DECRYPT); DECRYPT.addActionListener(this);
// top.add(GENKEY); GENKEY.addActionListener(this);
top.add(LOADKEY); LOADKEY.addActionListener(this);
top.add(SAVEKEY); SAVEKEY.addActionListener(this);
top.add(infoLabel);
top.add(QUIT); QUIT.addActionListener(this);
cp.add(top, BorderLayout.NORTH);
JPanel bottom = new JPanel(new FlowLayout());
bottom.add(statusLabel);
cp.add(bottom, BorderLayout.SOUTH);
addWindowListener(new ExitController());
setTitle(TITLE);
setSize(width,height);
setVisible(true);
this.cs = cs;
if (cs != null)
{
setInfo("Using: " + this.cs);
sk = cs.generateKey();
pk = sk.getPublic();
}
else setInfo("No CS loaded");
setStatus("Ready");
System.out.println("[OK]");
System.out.println("Using: " + cs);
System.out.println("Ready");
}
catch(Exception e)
{
pane.setText("\n\tError\n\tE GUI-error in " + TITLE);
}
}
public void setStatus(String s)
{
statusLabel.setText(s);
statusLabel.repaint();
try
{
Thread.sleep(10);
} catch(InterruptedException e){;}
}
public void setInfo(String s)
{
infoLabel.setText(s);
infoLabel.repaint();
try {Thread.sleep(10);} catch(InterruptedException e){;}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == QUIT)
{
System.out.println("Exiting " + TITLE);
System.exit(0);
}
else if (e.getSource() == ENCRYPT)
{
encrypt();
this.setVisible(false);
try {Thread.sleep(250);} catch(InterruptedException E){;}
ENCRYPT.setVisible(false);
LOADKEY.setVisible(false);
SAVEKEY.setVisible(false);
this.repaint();
this.setVisible(true);
}
else if (e.getSource() == DECRYPT)
{
this.setVisible(false);
decrypt();
try{ Thread.sleep(250);} catch(InterruptedException E){;}
this.setVisible(true);
}
else if (e.getSource() == LOADKEY) {loadKey();}
else if (e.getSource() == SAVEKEY) {saveKey();}
}
public File openFile()
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
targetFile= chooser.getSelectedFile();
Path=targetFile.getPath();
System.out.println("\nPath is "+Path);
return targetFile;
}
else
return null;
}
public File saveFile()
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
targetFile= chooser.getSelectedFile();
Path=targetFile.getPath();
return targetFile;
}
else return null;
}
private void encrypt()
{
File f = new File(Path);
if (f == null)
{
new JOptionPane("No file selected for encryption!");
return;
}
try
{
InputStream in = new FileInputStream(f);
OutputStream out = new CryptoOutputStream(new FileOutputStream(new File(f.getParent(),f.getName() + ".enc")), cs, pk);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String s;
int read;
setStatus("Encrypting: " + f.getName() + " -> " + "encrypted.txt ...");
System.out.print("Encrypting: " + f.getName() + " -> " + "encrypted.txt ...");
//For storing encrypted text in encrypted.txt file
OutputStream eout = new FileOutputStream(new File(f.getParent(),"encrypted.txt"));
int bytes = 0;
while((read = in.read()) != -1)
{
bout.write(read);
out.write(read);
bytes++;
}
out.flush();
in.close();
out.close();
setStatus("done");
InputStream enc = new FileInputStream(new File(f.getParent(),f.getName() + ".enc"));
ByteArrayOutputStream encout = new ByteArrayOutputStream();
while((read = enc.read()) != -1)
{
encout.write(read);
}
String encrypted=encout.toString();
String plain=bout.toString();
String printed="";
printed=encrypted;
eout.write(printed.getBytes());
eout.flush();
eout.close();
s = "Plain Text: " + plain + "\n" + "Encrypted Text:\n "+ printed;
pane.setFont(new Font("TimesNewRoman", Font.BOLD, 12));
pane.setText(s);
enc.close();
System.out.println("OK");
}
catch(IOException e)
{
System.out.println("Error in encryption!");
}
}
private void decrypt()
{
Path=Path+".enc";
File f = new File(Path);
if (f == null)
{
new JOptionPane("No file selected for decryption!");
return;
}
try
{
InputStream in = new CryptoInputStream(new FileInputStream(f), cs, sk);
OutputStream out = new FileOutputStream(new File(f.getParent(),f.getName().substring(0, f.getName().length()-4) + ".dec"));
System.out.print("Decrypting: " + f.getName() + " -> " + f.getName().substring(0, f.getName().length()-4) + ".dec ...");
setStatus("Decrypting: " + f.getName() + " -> " + f.getName().substring(0, f.getName().length()-4) + ".dec ...");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String s;
int bytes = 0;
int read;
while((read = in.read()) != -1)
{
out.write(read);
bout.write(read);
bytes++;
}
s = "Decrypted Text: " + bout.toString();
pane.setFont(new Font("TimesNewRoman", Font.BOLD, 12));
pane.setText(s);
out.flush();
in.close();
out.close();
System.out.println("OK");
setStatus("done");
}
catch(Exception e)
{
System.out.println("Error in decryption!");
}
}
private void loadKey()
{
File f = openFile();
if (f==null)
{
new JOptionPane("No file selected for key");
return;
}
try
{
sk=readKey(f);
System.out.println("sk is:" + sk);
pk=sk.getPublic();
}
catch(Exception e)
{
System.out.println("Error loading key!");
e.printStackTrace();
}
}
private void saveKey()
{
/*File f = saveFile();
if (f==null)
{
new JOptionPane("No file selected for key");
return;
}
File fsk=new File(f.getName()+"_secret.txt");
System.out.println("Filename :"+fsk);
File fpk=new File(f.getName()+"_public.txt");
System.out.println("Filename 2: "+fpk);
try
{
//if (!sk.isPublic())
writeKey(fsk,sk);
writeKey(fpk,pk);
}
catch(Exception e)
{
System.out.println("Error in decryption!");
}*/
String keydisp=" "+sk+"\n"+" "+pk;
pane.setFont(new Font("TimesNewRoman", Font.BOLD, 12));
pane.setText(keydisp);
}
/** Writes the Key instance to a File f*/
public void writeKey(File f, Key k)
{
try
{
FileOutputStream out=new FileOutputStream(f);
k.writeKey(out);
out.flush();
out.close();
}
catch(IOException e)
{
System.out.println("Error writing key!\n");
e.printStackTrace(System.out);
System.exit(0);
}
}
public Key readKey(File f)
{
try
{
Key k=cs.generateKey();
FileInputStream in = new FileInputStream(f);
k=k.readKey(in);
in.close();
return k;
}
catch(IOException e)
{
System.out.println("Error reading key file!\n" + e);
return null;
}
}
public class ExitController extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.out.println("Exiting " + TITLE);
System.exit(0);
}
}
}