-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageOperation.java
More file actions
142 lines (122 loc) · 4.41 KB
/
ImageOperation.java
File metadata and controls
142 lines (122 loc) · 4.41 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
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageOperation {
static File file = null;
static boolean isEncrypted(File file) {
try {
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(file);
if (image == null)
return true;
} catch (Exception e) {
return true;
}
return false;
}
static void operation(int key, File file, boolean doEncrypt) {
try {
// encryption & decryption logic
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[fis.available()];
fis.read(data);
int i = 0;
for (byte b : data) {
data[i] = (byte) (b ^ key);
i++;
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
fis.close();
String message;
if (doEncrypt)
message = "file Encrypted successfully.";
else
message = "file Decrypted successfully.";
JOptionPane.showMessageDialog(null, message);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// creating a frame
JFrame f = new JFrame();
f.setTitle("Image Encryption/Decryption");
f.setSize(300, 300);
f.setLocationRelativeTo(null);
// choose file button
JButton cf = new JButton();
cf.setText("choose file");
// Encryption button
JButton enc = new JButton();
enc.setText("Encrypt");
// Decryption button
JButton dec = new JButton();
dec.setText("Decrypt");
JLabel label = new JLabel("Key:");
// creating text field
JTextField tf = new JTextField(10);
// button listeners
cf.addActionListener(e -> {
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png"));
fc.setAcceptAllFileFilterUsed(false);
fc.showOpenDialog(null);
file = fc.getSelectedFile();
});
enc.addActionListener(e -> {
try {
String val = tf.getText();
int key = Integer.parseInt(val);
if (file == null) {
JOptionPane.showMessageDialog(null, "please select a file!", "Error", JOptionPane.ERROR_MESSAGE);
} else {
if (isEncrypted(file)) {
JOptionPane.showMessageDialog(null, "File is already Encrypted!");
} else {
operation(key, file, true);
}
}
} catch (NumberFormatException no_key) {
JOptionPane.showMessageDialog(null, "key cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
}
});
dec.addActionListener(e -> {
try {
String val = tf.getText();
int key = Integer.parseInt(val);
if (file == null) {
JOptionPane.showMessageDialog(null, "please select a file!", "Error", JOptionPane.ERROR_MESSAGE);
} else {
if (isEncrypted(file)) {
operation(key, file, false);
} else {
JOptionPane.showMessageDialog(null, "File is already Decrypted!");
}
}
} catch (NumberFormatException no_key) {
JOptionPane.showMessageDialog(null, "key cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
}
});
f.setLayout(new FlowLayout());
f.add(label);
f.add(tf);
f.add(cf);
f.add(enc);
f.add(dec);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}