-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXORFileEncrypt-Decrypt.java
More file actions
99 lines (86 loc) · 3.35 KB
/
Copy pathXORFileEncrypt-Decrypt.java
File metadata and controls
99 lines (86 loc) · 3.35 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xor_crypt;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class XORFileEncryptorGUI extends JFrame {
private JTextField keyField;
private JButton encryptButton, decryptButton;
private JFileChooser fileChooser;
public XORFileEncryptorGUI() {
setTitle("XOR File Encryptor");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
keyField = new JTextField(20);
encryptButton = new JButton("Encrypt File");
decryptButton = new JButton("Decrypt File");
fileChooser = new JFileChooser();
add(new JLabel("Key:"));
add(keyField);
add(encryptButton);
add(decryptButton);
encryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processFile(true);
}
});
decryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processFile(false);
}
});
}
private void processFile(boolean encrypt) {
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
String key = keyField.getText();
if (key.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a key", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
byte[] fileData = readFile(file);
byte[] keyBytes = key.getBytes();
byte[] processedData = xorEncryptDecrypt(fileData, keyBytes);
writeFile(new File(file.getAbsolutePath() + (encrypt ? ".enc" : ".dec")), processedData);
JOptionPane.showMessageDialog(this, "File processed successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Error processing file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private byte[] xorEncryptDecrypt(byte[] data, byte[] key) {
byte[] result = new byte[data.length];
for (int i = 0; i < data.length; i++) {
result[i] = (byte) (data[i] ^ key[i % key.length]);
}
return result;
}
private byte[] readFile(File file) throws IOException {
try (FileInputStream fis = new FileInputStream(file)) {
byte[] data = new byte[(int) file.length()];
fis.read(data);
return data;
}
}
private void writeFile(File file, byte[] data) throws IOException {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(data);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
XORFileEncryptorGUI frame = new XORFileEncryptorGUI();
frame.setVisible(true);
});
}
}