-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAffineCipherTool.java
More file actions
167 lines (149 loc) · 6.07 KB
/
AffineCipherTool.java
File metadata and controls
167 lines (149 loc) · 6.07 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class AffineCipherTool {
private static int key1;
private static int key2;
private static final int MOD = 26;
public static void main(String[] args) {
SwingUtilities.invokeLater(AffineCipherTool::createGUI);
}
private static void createGUI() {
JFrame frame = new JFrame("Affine Cipher File Encryption");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLayout(new GridLayout(3, 1));
JButton selectFileButton = new JButton("Select File");
JButton encryptButton = new JButton("Encrypt");
JButton decryptButton = new JButton("Decrypt");
JButton closeButton = new JButton("Close");
JLabel statusLabel = new JLabel("Select a file to encrypt");
frame.add(selectFileButton);
frame.add(encryptButton);
frame.add(statusLabel);
frame.add(closeButton);
decryptButton.setVisible(false);
final File[] selectedFile = { null };
final String[] encryptedFilePath = { null };
selectFileButton.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
selectedFile[0] = fileChooser.getSelectedFile();
statusLabel.setText("Selected File: " + selectedFile[0].getName());
}
});
encryptButton.addActionListener(e -> {
if (selectedFile[0] == null) {
statusLabel.setText("Please select a file first!");
return;
}
try {
key1 = generateValidKey1();
key2 = (int) (Math.random() * MOD);
String content = readFile(selectedFile[0].getAbsolutePath());
String encryptedText = encrypt(content);
String encPath = selectedFile[0].getAbsolutePath() + ".enc";
writeFile(encPath, encryptedText);
encryptedFilePath[0] = encPath;
statusLabel.setText(
"File Encrypted! k1=" + key1 + ", k2=" + key2 + ". Click Decrypt to generate .dec file.");
// Switch UI to show Decrypt button
frame.remove(encryptButton);
frame.add(decryptButton);
decryptButton.setVisible(true);
frame.revalidate();
frame.repaint();
} catch (Exception ex) {
statusLabel.setText("Error: " + ex.getMessage());
}
});
decryptButton.addActionListener(e -> {
if (encryptedFilePath[0] == null) {
statusLabel.setText("No encrypted file found!");
return;
}
try {
String encryptedText = readFile(encryptedFilePath[0]);
String decryptedText = decrypt(encryptedText);
writeFile(encryptedFilePath[0] + ".dec", decryptedText);
statusLabel.setText("File Decrypted! .dec file generated. You may close the window.");
// Show Close button
frame.add(closeButton);
closeButton.setVisible(true);
frame.revalidate();
frame.repaint();
} catch (Exception ex) {
statusLabel.setText("Error: " + ex.getMessage());
}
});
closeButton.addActionListener(e -> {
System.exit(0);
});
frame.setVisible(true);
}
// Generate a valid key1 (coprime with MOD=26)
private static int generateValidKey1() {
int[] coprimes = { 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 };
int idx = (int) (Math.random() * coprimes.length);
return coprimes[idx];
}
private static String readFile(String filePath) throws IOException {
StringBuilder content = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
reader.close();
return content.toString();
}
private static void writeFile(String filePath, String content) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
writer.write(content);
writer.close();
}
public static String encrypt(String plaintext) {
StringBuilder ciphertext = new StringBuilder();
for (char character : plaintext.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
int k3 = character - base;
int encrypted = (k3 * key1 + key2) % MOD;
ciphertext.append((char) (encrypted + base));
} else {
ciphertext.append(character);
}
}
return ciphertext.toString();
}
public static String decrypt(String ciphertext) {
StringBuilder plaintext = new StringBuilder();
int k1_inverse = findModInverse(key1, MOD);
if (k1_inverse == -1) {
return "Error: Invalid Key1! Must be coprime with 26.";
}
for (char character : ciphertext.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
int k3 = character - base;
int decrypted = ((k3 - key2 + MOD) * k1_inverse) % MOD;
plaintext.append((char) (decrypted + base));
} else {
plaintext.append(character);
}
}
return plaintext.toString();
}
private static int findModInverse(int a, int m) {
a = a % m;
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return -1;
}
}