-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileEncryptDecryptApp.java
182 lines (145 loc) · 6.97 KB
/
FileEncryptDecryptApp.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
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileEncryptDecryptApp extends JFrame {
private JButton encryptButton;
private JButton decryptButton;
private JTextArea logTextArea;
private File selectedFile;
private JFileChooser fileChooser;
public FileEncryptDecryptApp() {
// Initialize UI components and layout
this.setTitle("File Encryption/Decryption App");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 400);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
fileChooser.setFileFilter(filter);
logTextArea = new JTextArea();
logTextArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(logTextArea);
panel.add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
encryptButton = new JButton("Encrypt");
decryptButton = new JButton("Decrypt");
encryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectFile();
if (selectedFile != null) {
try {
String inputFilePath = selectedFile.getAbsolutePath();
byte[] fileData = Files.readAllBytes(Paths.get(inputFilePath));
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey aesKey = keyGen.generateKey();
byte[] encryptedData = encryptAES(fileData, aesKey);
String outputFilePath = inputFilePath + ".enc";
Files.write(Paths.get(outputFilePath), encryptedData);
// Encrypt AES key using RSA public key
PublicKey rsaPublicKey = loadRSAPublicKey("publickey.pem"); // Replace with your key file
byte[] encryptedAESKey = encryptRSA(aesKey.getEncoded(), rsaPublicKey);
saveKeyToFile("cipherkey.json", encryptedAESKey);
logTextArea.append("Encryption complete. Encrypted file saved as: " + outputFilePath + "\n");
} catch (Exception ex) {
logTextArea.append("Encryption failed. " + ex.getMessage() + "\n");
}
}
}
});
decryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectFile();
if (selectedFile != null) {
try {
String inputFilePath = selectedFile.getAbsolutePath();
byte[] encryptedData = Files.readAllBytes(Paths.get(inputFilePath));
// Decrypt AES key using RSA private key
PrivateKey rsaPrivateKey = loadRSAPrivateKey("privatekey.pem"); // Replace with your key file
byte[] aesKeyBytes = decryptRSA(loadKeyFromFile("cipherkey.json"), rsaPrivateKey);
SecretKey aesKey = new SecretKeySpec(aesKeyBytes, 0, aesKeyBytes.length, "AES");
byte[] decryptedData = decryptAES(encryptedData, aesKey);
String outputFilePath = inputFilePath.replace(".enc", "_dec.txt");
Files.write(Paths.get(outputFilePath), decryptedData);
logTextArea.append("Decryption complete. Decrypted file saved as: " + outputFilePath + "\n");
} catch (Exception ex) {
logTextArea.append("Decryption failed. " + ex.getMessage() + "\n");
}
}
}
});
buttonPanel.add(encryptButton);
buttonPanel.add(decryptButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
this.add(panel);
}
private void selectFile() {
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
}
}
private byte[] encryptAES(byte[] data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
private byte[] decryptAES(byte[] data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
}
private byte[] encryptRSA(byte[] data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
private byte[] decryptRSA(byte[] data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
private PublicKey loadRSAPublicKey(String publicKeyFilePath) throws Exception {
byte[] publicKeyBytes = loadKeyFromFile(publicKeyFilePath);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
private PrivateKey loadRSAPrivateKey(String privateKeyFilePath) throws Exception {
byte[] privateKeyBytes = loadKeyFromFile(privateKeyFilePath);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
private byte[] loadKeyFromFile(String keyFilePath) throws IOException {
Path path = Paths.get(keyFilePath);
return Files.readAllBytes(path);
}
private void saveKeyToFile(String keyFilePath, byte[] keyData) throws IOException {
Path path = Paths.get(keyFilePath);
Files.write(path, keyData);
}
public static void main(String[] args) {
Security.addProvider(new BouncyCastleProvider());
SwingUtilities.invokeLater(() -> {
FileEncryptDecryptApp app = new FileEncryptDecryptApp();
app.setVisible(true);
});
}
}