-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageEncryptionApp.java
More file actions
217 lines (186 loc) · 7.23 KB
/
ImageEncryptionApp.java
File metadata and controls
217 lines (186 loc) · 7.23 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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.swing.*;
public class ImageEncryptionApp {
private JFrame frame;
private JButton encryptButton;
private JButton decryptButton;
private JButton selectFileButton;
private JLabel selectedFileLabel;
private JLabel keyLabel;
private JTextField keyField;
private File selectedFile;
private File encryptedFile;
private File decryptedFile;
private SecretKey encryptionKey;
public ImageEncryptionApp() {
frame = new JFrame("Image Encryption and Decryption");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 400);
frame.setContentPane(new ImagePanel());
selectedFileLabel = new JLabel("Selected File");
keyLabel = new JLabel("Encryption Key");
keyField = new JTextField(20);
selectFileButton = new JButton("Select Image");
encryptButton = new JButton("Encrypt");
decryptButton = new JButton("Decrypt");
// Set button backgrounds and text color
selectFileButton.setBackground(Color.BLACK);
selectFileButton.setForeground(Color.BLACK);
encryptButton.setBackground(Color.BLACK);
encryptButton.setForeground(Color.BLACK);
decryptButton.setBackground(Color.BLACK);
decryptButton.setForeground(Color.BLACK);
// Set font and style for labels
Font labelFont = new Font(
selectedFileLabel.getFont().getName(),
Font.BOLD,
30);
selectedFileLabel.setFont(labelFont);
selectedFileLabel.setForeground(Color.black);
keyLabel.setFont(labelFont);
keyLabel.setForeground(Color.darkGray);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(selectedFileLabel, gbc);
gbc.gridy++;
frame.add(keyLabel, gbc);
gbc.gridy++;
frame.add(keyField, gbc);
gbc.gridy++;
frame.add(selectFileButton, gbc);
gbc.gridy++;
frame.add(encryptButton, gbc);
gbc.gridy++;
frame.add(decryptButton, gbc);
selectFileButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
selectedFileLabel.setText(
"Selected File: " + selectedFile.getName());
}
}
});
encryptButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
encryptFile();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
decryptButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
decryptFile();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
frame.setVisible(true);
}
public void encryptFile()
throws NoSuchAlgorithmException, InvalidKeyException {
if (selectedFile == null || !selectedFile.exists()) {
return;
}
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // 128-bit key for AES encryption
encryptionKey = keyGen.generateKey(); // Store the key for decryption
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
FileInputStream input = new FileInputStream(selectedFile);
encryptedFile = new File(
selectedFile.getParentFile(),
"encrypted_" + selectedFile.getName() + ".txt");
FileOutputStream output = new FileOutputStream(encryptedFile);
CipherOutputStream cipherOutput = new CipherOutputStream(output, cipher);
int b;
while ((b = input.read()) != -1) {
cipherOutput.write(b);
}
input.close();
cipherOutput.close();
selectedFileLabel.setText("Selected File: Encrypted");
} catch (Exception e) {
e.printStackTrace();
}
}
public void decryptFile()
throws NoSuchAlgorithmException, InvalidKeyException {
if (encryptedFile == null || !encryptedFile.exists()) {
return;
}
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, encryptionKey); // Use the stored key
encryptedFile = new File(
selectedFile.getParentFile(),
"encrypted_" + selectedFile.getName() + ".txt");
decryptedFile = new File(
selectedFile.getParentFile(),
"decrypted_" + selectedFile.getName());
FileInputStream input = new FileInputStream(encryptedFile);
FileOutputStream output = new FileOutputStream(decryptedFile);
CipherInputStream cipherInput = new CipherInputStream(input, cipher);
int b;
while ((b = cipherInput.read()) != -1) {
output.write(b);
}
input.close();
output.close();
cipherInput.close();
selectedFileLabel.setText("Selected File: Decrypted");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new ImageEncryptionApp();
});
}
// Custom JPanel for setting the background image
class ImagePanel extends JPanel {
private Image backgroundImg;
public ImagePanel() {
backgroundImg =
new ImageIcon(".\\background.jpg").getImage();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(
backgroundImg,
0,
0,
this.getWidth(),
this.getHeight(),
this);
}
}
}
}