-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageWin.java
More file actions
255 lines (190 loc) · 6.08 KB
/
ImageWin.java
File metadata and controls
255 lines (190 loc) · 6.08 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
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.File;
class ImageWin extends JFrame implements ActionListener{
/** Image Display panel */
private ImageRead panel;
/** Image encrypter class */
private ImageEncrypt encrypter;
/** Internal filename */
private File fileName;
/** Constructor */
public ImageWin(){
panel = new ImageRead();
getContentPane().add(panel);
pack();
setSize(300,300);
//Construct the menu
setJMenuBar(constructMenu());
encrypter = new ImageEncrypt();
encrypter.test();
}
/** Extention of the constructor for menu creation */
private JMenuBar constructMenu(){
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(new JMenuItem("New Blank Image ..."));
menuBar.add(fileMenu);
fileMenu.add(new JMenuItem("Open ..."));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem("Save"));
fileMenu.add(new JMenuItem("Save As..."));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem("Quit"));
// Add the listeners
for(int i=0;i<fileMenu.getItemCount();i++){
JMenuItem me = fileMenu.getItem(i);
if(me != null) me.addActionListener(this);
}
JMenu editMenu = new JMenu("Cipher");
menuBar.add(editMenu);
editMenu.add(new JMenuItem("Set Key"));
editMenu.addSeparator();
editMenu.add(new JMenuItem("Encrypt"));
editMenu.add(new JMenuItem("Decrypt"));
editMenu.addSeparator();
editMenu.add(new JMenuItem("Encrypt w/ Block Trick"));
editMenu.add(new JMenuItem("Decrypt w/ Block Trick"));
editMenu.addSeparator();
editMenu.add(new JMenuItem("Two Image Attack..."));
// Add the listeners
for(int i=0;i<editMenu.getItemCount();i++){
JMenuItem me = editMenu.getItem(i);
if(me != null) me.addActionListener(this);
}
return menuBar;
}
/** Sets the image file being used and changes the frame title to match
* @param file the file for the imag
*/
private void setFile(File file){
fileName = file;
setTitle("JImageCipher ~ " + file.getName());
}
/** Action listener **/
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();
try{
// File actions
if(text =="New Blank Image ..."){ actionNewBlank(); }
else if(text == "Open ..."){ actionLoadImage(null); }
else if(text == "Save"){ actionSaveImage(fileName); }
else if(text == "Save As..."){ actionSaveImage(null);}
else if(text == "Quit"){ System.exit(0);}
// Edit actions
else if(text == "Set Key"){
actionKeyDialog();
}else if(text == "Encrypt"){
panel.setImage(encrypter.map(panel.getImage(),true,false));
}else if(text == "Decrypt"){
panel.setImage(encrypter.map(panel.getImage(),false,false));
}else if(text == "Encrypt w/ Block Trick"){
panel.setImage(encrypter.map(panel.getImage(),true,true));
}else if(text == "Decrypt w/ Block Trick"){
panel.setImage(encrypter.map(panel.getImage(),false,true));
}else if(text == "Two Image Attack..."){
actionAttack();
}
}catch(Exception err){ System.out.println("ERROR:" + err);}
}
/** Action call-back */
public void actionAttack() throws Exception{
JFileChooser fc = new JFileChooser(fileName);
fc.showOpenDialog(this);
File imageFile1 = fc.getSelectedFile();
if(imageFile1 == null){return;}
fc.showOpenDialog(this);
File imageFile2 = fc.getSelectedFile();
if(imageFile2 == null){return;}
BufferedImage img1 = imageFromFile(imageFile1);
BufferedImage img2 = imageFromFile(imageFile2);
BufferedImage attack = encrypter.attack(img1,img2);
panel.setImage(attack);
setFile(new File("attack.png"));
}
/** Set the key **/
public void actionKeyDialog(){
String key = new String(encrypter.getKey());
key = (String)JOptionPane.showInputDialog(this,
"Enter a 16 byte key (current key= " +
key.getBytes().length + " bytes)",key);
while(key != null && key.getBytes().length != 16){
key = (String)JOptionPane.showInputDialog(this,
"Enter a 16 byte key (current key= " +
key.getBytes().length + " bytes)",key);
}
if(key != null) encrypter.setKey(key.getBytes());
}
/** Loads a new blank Image */
public void actionNewBlank(){
Color newColor = JColorChooser.showDialog(
this,
"Choose Fill Color",
Color.white);
if(newColor != null){
panel.setImage(encrypter.blankImage(newColor,
panel.getImage().getWidth(),
panel.getImage().getHeight()));
setFile( new File("blank.png"));
}
}
/** Load an image from a file
* @param file the name of the file to load, use "null" to access a dialog
*/
public void actionLoadImage(File imageFile){
if(imageFile == null){
JFileChooser fc = new JFileChooser(fileName);
fc.showOpenDialog(this);
imageFile = fc.getSelectedFile();
}
if(imageFile != null){
panel.setImage(imageFromFile(imageFile));
setFile(imageFile);
}
}
/** Load an image from a file
* @param file the filename
* @return the buffered image, "null" if file did not exist
*/
private BufferedImage imageFromFile(File file){
System.out.println("Loading File ... " + file.getName());
BufferedImage img = null;
try{
img = ImageIO.read(file);
}catch(Exception e){
System.out.println("Error:" + e);
}
return img;
}
/** Save an image from a file
* @param file the name of the file to save, use "null" to access a dialog
*/
public void actionSaveImage(File imageFile){
if(imageFile == null){
JFileChooser fc = new JFileChooser(fileName);
fc.showSaveDialog(this);
imageFile = fc.getSelectedFile();
}
if(imageFile != null){
System.out.println("Saving File ... " + imageFile.getName());
try{
ImageIO.write(panel.getImage(), "png", imageFile);
}catch(Exception e){
System.out.println("Error:" + e);
}
setFile(imageFile);
}
}
/** Main function **/
public static void main(String args[])
{
ImageWin win = new ImageWin();
win.setVisible(true);
if(args.length > 0){
win.actionLoadImage(new File(args[0]));
}
}
}