-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureCrypt.java
More file actions
487 lines (416 loc) · 19.7 KB
/
SecureCrypt.java
File metadata and controls
487 lines (416 loc) · 19.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class SecureCrypt extends JFrame {
private JTextArea inputArea, outputArea;
private JComboBox<String> algoBox;
private JButton encryptBtn, decryptBtn;
private JPasswordField passwordField;
private JCheckBox showPasswordCheckBox;
private static final String AES_KEY = "1234567890123456";
private static final int GCM_TAG_LENGTH = 128;
private static final int GCM_IV_LENGTH = 12;
public SecureCrypt() {
setTitle("🔐 Encryption / Decryption Tool");
setSize(700, 550);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel background = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp = new GradientPaint(0, 0, new Color(135, 206, 250), 0, getHeight(), new Color(70, 130, 180));
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
};
background.setLayout(new BorderLayout());
setContentPane(background);
inputArea = new JTextArea(5, 40);
outputArea = new JTextArea(5, 40);
outputArea.setEditable(false);
JScrollPane inputScroll = new JScrollPane(inputArea);
JScrollPane outputScroll = new JScrollPane(outputArea);
inputScroll.setBorder(BorderFactory.createTitledBorder("📥 Input Text"));
outputScroll.setBorder(BorderFactory.createTitledBorder("📤 Output Text"));
String[] algos = {
"Caesar Cipher", "Base64", "AES", "Reverse Text", "ROT13",
"XOR Cipher", "Atbash Cipher", "Vigenere Cipher", "Hex Encode", "Substitution Cipher"
};
algoBox = new JComboBox<>(algos);
passwordField = new JPasswordField(15);
showPasswordCheckBox = new JCheckBox("Show Password");
JPanel passPanel = new JPanel();
passPanel.setOpaque(false);
passPanel.add(new JLabel("🔑 Master Password (optional): "));
passPanel.add(passwordField);
passPanel.add(showPasswordCheckBox);
encryptBtn = new JButton("🔒 Encrypt");
decryptBtn = new JButton("🔓 Decrypt");
JPanel topPanel = new JPanel(new GridLayout(2,1));
topPanel.setOpaque(false);
JPanel algoPanel = new JPanel();
algoPanel.setOpaque(false);
algoPanel.add(new JLabel("🎯 Algorithm: "));
algoPanel.add(algoBox);
topPanel.add(algoPanel);
topPanel.add(passPanel);
JPanel btnPanel = new JPanel();
btnPanel.setOpaque(false);
btnPanel.add(encryptBtn);
btnPanel.add(decryptBtn);
JPanel centerPanel = new JPanel(new GridLayout(2, 1, 10, 10));
centerPanel.setOpaque(false);
centerPanel.add(inputScroll);
centerPanel.add(outputScroll);
background.add(topPanel, BorderLayout.NORTH);
background.add(centerPanel, BorderLayout.CENTER);
background.add(btnPanel, BorderLayout.SOUTH);
setupMenuBar();
setupEventListeners();
}
private void setupMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("📁 File");
JMenuItem saveItem = new JMenuItem("💾 Save");
JMenuItem loadItem = new JMenuItem("📂 Load");
fileMenu.add(saveItem);
fileMenu.add(loadItem);
menuBar.add(fileMenu);
// Add Help menu with About
JMenu helpMenu = new JMenu("❓ Help");
JMenuItem aboutItem = new JMenuItem("ℹ️ About");
helpMenu.add(aboutItem);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
// Menu actions
saveItem.addActionListener(e -> saveToFile());
loadItem.addActionListener(e -> loadFromFile());
aboutItem.addActionListener(e -> showAboutDialog());
}
private void setupEventListeners() {
// Show password checkbox listener
showPasswordCheckBox.addActionListener(e -> {
if (showPasswordCheckBox.isSelected()) {
passwordField.setEchoChar((char) 0); // Show password
} else {
passwordField.setEchoChar('•'); // Hide password
}
});
encryptBtn.addActionListener(e -> {
String input = inputArea.getText();
String algo = (String) algoBox.getSelectedItem();
try {
String result = encrypt(input, algo);
outputArea.setText(result);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
});
decryptBtn.addActionListener(e -> {
String input = inputArea.getText();
String algo = (String) algoBox.getSelectedItem();
try {
String result = decrypt(input, algo);
outputArea.setText(result);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
});
}
private void saveToFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write("Input:\n" + inputArea.getText() + "\n\n");
writer.write("Output:\n" + outputArea.getText() + "\n");
JOptionPane.showMessageDialog(this, "File saved successfully!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Error saving file: " + ex.getMessage());
}
}
}
private void loadFromFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
inputArea.setText(content.toString().trim());
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Error loading file: " + ex.getMessage());
}
}
}
private void showAboutDialog() {
// Create a centered about dialog
JPanel aboutPanel = new JPanel();
aboutPanel.setLayout(new BoxLayout(aboutPanel, BoxLayout.Y_AXIS));
aboutPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("SecureCrypt");
titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Version
JLabel versionLabel = new JLabel("Version 1.0");
versionLabel.setFont(new Font("Arial", Font.BOLD, 14));
versionLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Description
JLabel descLabel = new JLabel("A powerful encryption/decryption tool");
descLabel.setFont(new Font("Arial", Font.PLAIN, 12));
descLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Algorithms label
JLabel algoLabel = new JLabel("Supported Algorithms:");
algoLabel.setFont(new Font("Arial", Font.BOLD, 12));
algoLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Algorithms list
JTextArea algoArea = new JTextArea();
algoArea.setText("• Caesar Cipher\n• Base64\n• AES\n• Reverse Text\n• ROT13\n• XOR Cipher\n• Atbash Cipher\n• Vigenere Cipher\n• Hex Encode\n• Substitution Cipher");
algoArea.setFont(new Font("Arial", Font.PLAIN, 11));
algoArea.setBackground(aboutPanel.getBackground());
algoArea.setEditable(false);
algoArea.setAlignmentX(Component.CENTER_ALIGNMENT);
// Copyright
JLabel copyrightLabel = new JLabel("© 2025 All rights reserved");
copyrightLabel.setFont(new Font("Arial", Font.ITALIC, 10));
copyrightLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Add components with spacing
aboutPanel.add(titleLabel);
aboutPanel.add(Box.createRigidArea(new Dimension(0, 10)));
aboutPanel.add(versionLabel);
aboutPanel.add(Box.createRigidArea(new Dimension(0, 5)));
aboutPanel.add(descLabel);
aboutPanel.add(Box.createRigidArea(new Dimension(0, 15)));
aboutPanel.add(algoLabel);
aboutPanel.add(Box.createRigidArea(new Dimension(0, 5)));
aboutPanel.add(algoArea);
aboutPanel.add(Box.createRigidArea(new Dimension(0, 15)));
aboutPanel.add(copyrightLabel);
JOptionPane.showMessageDialog(this, aboutPanel, "ℹ️ About", JOptionPane.INFORMATION_MESSAGE);
}
// FIXED: Encryption methods now work without password
private String encrypt(String text, String algo) throws Exception {
// Apply algorithm first, then master password if provided
String encryptedText = applyAlgorithm(text, algo, true);
return applyMasterPassword(encryptedText, true);
}
private String decrypt(String text, String algo) throws Exception {
// Remove master password first if provided, then apply algorithm
String withoutMaster = applyMasterPassword(text, false);
return applyAlgorithm(withoutMaster, algo, false);
}
private String applyAlgorithm(String text, String algo, boolean encrypt) throws Exception {
switch (algo) {
case "Caesar Cipher":
return encrypt ? caesarCipher(text, 3) : caesarCipher(text, -3);
case "Base64":
return encrypt ? Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8))
: new String(Base64.getDecoder().decode(text), StandardCharsets.UTF_8);
case "AES":
return encrypt ? aesEncrypt(text) : aesDecrypt(text);
case "Reverse Text":
return new StringBuilder(text).reverse().toString();
case "ROT13":
return rot13(text);
case "XOR Cipher":
return xorCipher(text, "SECRETKEY");
case "Atbash Cipher":
return atbash(text);
case "Vigenere Cipher":
return encrypt ? vigenereEncrypt(text, "KEY") : vigenereDecrypt(text, "KEY");
case "Hex Encode":
return encrypt ? hexEncode(text) : hexDecode(text);
case "Substitution Cipher":
return encrypt ? substitutionEncrypt(text) : substitutionDecrypt(text);
default:
return text;
}
}
private String applyMasterPassword(String text, boolean encrypting) throws Exception {
String pwd = new String(passwordField.getPassword());
if (pwd.isEmpty()) return text;
return aesWithPassword(text, pwd, encrypting);
}
private String aesWithPassword(String text, String password, boolean encrypt) throws Exception {
SecretKeySpec key = getAESKeyFromPassword(password);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
if (encrypt) {
byte[] iv = new byte[GCM_IV_LENGTH];
new SecureRandom().nextBytes(iv);
GCMParameterSpec parameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
byte[] encrypted = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
byte[] combined = new byte[iv.length + encrypted.length];
System.arraycopy(iv, 0, combined, 0, iv.length);
System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);
return Base64.getEncoder().encodeToString(combined);
} else {
byte[] combined = Base64.getDecoder().decode(text);
if (combined.length < GCM_IV_LENGTH) {
throw new IllegalArgumentException("Invalid encrypted data");
}
byte[] iv = new byte[GCM_IV_LENGTH];
byte[] encrypted = new byte[combined.length - GCM_IV_LENGTH];
System.arraycopy(combined, 0, iv, 0, iv.length);
System.arraycopy(combined, iv.length, encrypted, 0, encrypted.length);
GCMParameterSpec parameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
return new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8);
}
}
private SecretKeySpec getAESKeyFromPassword(String password) throws Exception {
MessageDigest sha = MessageDigest.getInstance("SHA-256");
byte[] key = sha.digest(password.getBytes(StandardCharsets.UTF_8));
return new SecretKeySpec(key, "AES");
}
private String aesEncrypt(String text) throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(StandardCharsets.UTF_8), "AES");
byte[] iv = new byte[GCM_IV_LENGTH];
new SecureRandom().nextBytes(iv);
GCMParameterSpec parameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
byte[] encrypted = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
byte[] combined = new byte[iv.length + encrypted.length];
System.arraycopy(iv, 0, combined, 0, iv.length);
System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);
return Base64.getEncoder().encodeToString(combined);
}
private String aesDecrypt(String text) throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(StandardCharsets.UTF_8), "AES");
byte[] combined = Base64.getDecoder().decode(text);
if (combined.length < GCM_IV_LENGTH) {
throw new IllegalArgumentException("Invalid encrypted data");
}
byte[] iv = new byte[GCM_IV_LENGTH];
byte[] encrypted = new byte[combined.length - GCM_IV_LENGTH];
System.arraycopy(combined, 0, iv, 0, iv.length);
System.arraycopy(combined, iv.length, encrypted, 0, encrypted.length);
GCMParameterSpec parameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
return new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8);
}
private String caesarCipher(String text, int shift) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isLowerCase(c) ? 'a' : 'A';
c = (char) ((c - base + shift + 26) % 26 + base);
}
result.append(c);
}
return result.toString();
}
private String rot13(String text) {
return caesarCipher(text, 13);
}
private String xorCipher(String text, String key) {
StringBuilder result = new StringBuilder();
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
char keyChar = (char) keyBytes[i % keyBytes.length];
result.append((char) (c ^ keyChar));
}
String rawResult = result.toString();
return Base64.getEncoder().encodeToString(rawResult.getBytes(StandardCharsets.UTF_8));
}
private String atbash(String text) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isUpperCase(c)) {
result.append((char) ('Z' - (c - 'A')));
} else if (Character.isLowerCase(c)) {
result.append((char) ('z' - (c - 'a')));
} else {
result.append(c);
}
}
return result.toString();
}
private String vigenereEncrypt(String text, String key) {
StringBuilder result = new StringBuilder();
key = key.toUpperCase();
int j = 0;
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
result.append((char) ((c - base + (key.charAt(j % key.length()) - 'A')) % 26 + base));
j++;
} else {
result.append(c);
}
}
return result.toString();
}
private String vigenereDecrypt(String text, String key) {
StringBuilder result = new StringBuilder();
key = key.toUpperCase();
int j = 0;
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
result.append((char) ((c - base - (key.charAt(j % key.length()) - 'A') + 26) % 26 + base));
j++;
} else {
result.append(c);
}
}
return result.toString();
}
private String hexEncode(String text) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
result.append(String.format("%02x", (int) c));
}
return result.toString();
}
private String hexDecode(String hex) {
hex = hex.replaceAll("[^0-9a-fA-F]", "");
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException("Invalid hex string length");
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < hex.length(); i += 2) {
String hexPair = hex.substring(i, i + 2);
result.append((char) Integer.parseInt(hexPair, 16));
}
return result.toString();
}
private String substitutionEncrypt(String text) {
String plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String subs = "QWERTYUIOPASDFGHJKLZXCVBNMmnbvcxzlkjhgfdsapoiuytrewq";
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
int idx = plain.indexOf(c);
result.append(idx >= 0 ? subs.charAt(idx) : c);
}
return result.toString();
}
private String substitutionDecrypt(String text) {
String plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String subs = "QWERTYUIOPASDFGHJKLZXCVBNMmnbvcxzlkjhgfdsapoiuytrewq";
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
int idx = subs.indexOf(c);
result.append(idx >= 0 ? plain.charAt(idx) : c);
}
return result.toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SecureCrypt().setVisible(true));
}
}