-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
364 lines (295 loc) · 12.4 KB
/
Copy pathMain.java
File metadata and controls
364 lines (295 loc) · 12.4 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
package Main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Define the authorized users and passwords
HashMap<String, String> authorizedUsers = new HashMap<>();
authorizedUsers.put("Alice", "password123");
authorizedUsers.put("Bob", "password456");
authorizedUsers.put("Charlie", "password789");
// Create the login screen
JFrame loginFrame = new JFrame("Login");
loginFrame.setSize(400, 300);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new GridLayout(3, 1));
JLabel usernameLabel = new JLabel("Username:");
JTextField usernameField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordField = new JPasswordField();
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (authorizedUsers.containsKey(username) && authorizedUsers.get(username).equals(password)) {
// User has been authenticated, show the main program window
loginFrame.dispose();
JFrame mainFrame = new JFrame("Encryption/Decryption Program");
mainFrame.setSize(400, 300);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
InputPanel inputPanel = new InputPanel();
OutputPanel outputPanel = new OutputPanel();
mainFrame.getContentPane().setLayout(new GridLayout(2, 1));
mainFrame.getContentPane().add(inputPanel);
mainFrame.getContentPane().add(outputPanel);
mainFrame.setVisible(true);
} else {
// Authentication failed, show an error message
JOptionPane.showMessageDialog(loginFrame, "Invalid username or password.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
JButton registerButton = new JButton("Register");
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Show the registration screen
loginFrame.dispose();
JFrame registerFrame = new JFrame("Registration");
registerFrame.setSize(400, 300);
registerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel registerPanel = new JPanel();
registerPanel.setLayout(new GridLayout(4, 1));
JLabel newUsernameLabel = new JLabel("New Username:");
JTextField newUsernameField = new JTextField();
JLabel newPasswordLabel = new JLabel("New Password:");
JPasswordField newPasswordField = new JPasswordField();
JButton registerUserButton = new JButton("Register User");
registerUserButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Add the new user to the authorized users and show the login screen
authorizedUsers.put(newUsernameField.getText(), new String(newPasswordField.getPassword()));
JOptionPane.showMessageDialog(registerFrame, "User successfully registered.", "Success", JOptionPane.PLAIN_MESSAGE);
registerFrame.dispose();
loginFrame.setVisible(true);
}
});
registerPanel.add(newUsernameLabel);
registerPanel.add(newUsernameField);
registerPanel.add(newPasswordLabel);
registerPanel.add(newPasswordField);
registerPanel.add(registerUserButton);
registerFrame.getContentPane().add(registerPanel);
registerFrame.setVisible(true);
}
});
loginPanel.add(usernameLabel);
loginPanel.add(usernameField);
loginPanel.add(passwordLabel);
loginPanel.add(passwordField);
loginPanel.add(loginButton);
loginPanel.add(registerButton);
loginFrame.getContentPane().add(loginPanel);
loginFrame.setVisible(true);
}
}
class Encryption {
public static String caesarEncrypt(String input, int key) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
if (Character.isUpperCase(ch)) {
result.append((char) ((ch + key - 65) % 26 + 65));
} else {
result.append((char) ((ch + key - 97) % 26 + 97));
}
} else {
result.append(ch);
}
}
return result.toString();
}
public static String railFenceEncrypt(String input, int key) {
char[][] matrix = new char[key][input.length()];
int row = 0, col = 0;
boolean down = true;
for (int i = 0; i < input.length(); i++) {
matrix[row][col] = input.charAt(i);
if (row == 0) {
down = true;
} else if (row == key - 1) {
down = false;
}
if (down) {
row++;
} else {
row--;
col++;
}
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < key; i++) {
for (int j = 0; j < input.length(); j++) {
if (matrix[i][j] != '\0') {
result.append(matrix[i][j]);
}
}
}
return result.toString();
}
public static String vigenereEncrypt(String input, String key) {
StringBuilder result = new StringBuilder();
int keyIndex = 0;
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
char shift = key.charAt(keyIndex);
if (Character.isUpperCase(shift)) {
shift = (char) (shift - 'A');
} else {
shift = (char) (shift - 'a');
}
if (Character.isUpperCase(ch)) {
result.append((char) ((ch + shift - 'A') % 26 + 'A'));
} else {
result.append((char) ((ch + shift - 'a') % 26 + 'a'));
}
keyIndex = (keyIndex + 1) % key.length();
} else {
result.append(ch);
}
}
return result.toString();
}
}
class Decryption {
public static String caesarDecrypt(String input, int key) {
return Encryption.caesarEncrypt(input, 26 - key);
}
public static String railFenceDecrypt(String input, int key) {
int[] rails = new int[input.length()];
int row = 0, col = 0;
boolean down = true;
for (int i = 0; i < input.length(); i++) {
rails[i] = row;
if (row == 0) {
down = true;
} else if (row == key - 1) {
down = false;
}
if (down) {
row++;
} else {
row--;
col++;
}
}
char[][] matrix = new char[key][input.length()];
int index = 0;
for (int i = 0; i < key; i++) {
for (int j = 0; j < input.length(); j++) {
if (rails[j] == i) {
matrix[i][j] = input.charAt(index++);
}
}
}
StringBuilder result = new StringBuilder();
row = 0;
col = 0;
down = true;
for (int i = 0; i < input.length(); i++) {
result.append(matrix[row][col]);
if (row == 0) {
down = true;
} else if (row == key - 1) {
down = false;
}
if (down) {
row++;
} else {
row--;
col++;
}
}
return result.toString();
}
public static String vigenereDecrypt(String input, String key) {
StringBuilder result = new StringBuilder();
for (int i = 0, j = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
if (Character.isUpperCase(ch)) {
result.append((char) ((ch - key.charAt(j) + 26) % 26 + 65));
} else {
result.append((char) ((ch - key.charAt(j) + 26) % 26 + 97));
}
j = (j + 1) % key.length();
} else {
result.append(ch);
}
}
return result.toString();
}
}
class InputPanel extends JPanel implements ActionListener {
private JTextField inputField;
private JButton encryptButton;
private JButton decryptButton;
private JComboBox<String> encryptionTypeComboBox;
public InputPanel() {
inputField = new JTextField(20);
encryptButton = new JButton("Encrypt");
decryptButton = new JButton("Decrypt");
encryptionTypeComboBox = new JComboBox<>(new String[]{"Caesar", "Rail Fence", "Vigenere"});
encryptionTypeComboBox.setSelectedIndex(0);
encryptButton.addActionListener(this);
decryptButton.addActionListener(this);
add(inputField);
add(encryptionTypeComboBox);
add(encryptButton);
add(decryptButton);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == encryptButton) {
String input = inputField.getText();
String output = "";
String encryptionType = (String) encryptionTypeComboBox.getSelectedItem();
if (encryptionType.equals("Caesar")) {
int key = Integer.parseInt(JOptionPane.showInputDialog("Enter key (an integer between 1 and 25)"));
output = Encryption.caesarEncrypt(input, key);
} else if (encryptionType.equals("Rail Fence")) {
int key = Integer.parseInt(JOptionPane.showInputDialog("Enter key (an integer between 2 and input length)"));
output = Encryption.railFenceEncrypt(input, key);
} else if (encryptionType.equals("Vigenere")) {
String key = JOptionPane.showInputDialog("Enter key (a word or phrase)");
output = Encryption.vigenereEncrypt(input, key);
}
OutputPanel.setOutput(output);
} else if (e.getSource() == decryptButton) {
String input = inputField.getText();
String output = "";
String encryptionType = (String) encryptionTypeComboBox.getSelectedItem();
if (encryptionType.equals("Caesar")) {
int key = Integer.parseInt(JOptionPane.showInputDialog("Enter key (an integer between 1 and 25)"));
output = Decryption.caesarDecrypt(input, key);
} else if (encryptionType.equals("Rail Fence")) {
int key = Integer.parseInt(JOptionPane.showInputDialog("Enter key (an integer between 2 and input length)"));
output = Decryption.railFenceDecrypt(input, key);
} else if (encryptionType.equals("Vigenere")) {
String key = JOptionPane.showInputDialog("Enter key (a word or phrase)");
output = Decryption.vigenereDecrypt(input, key);
}
OutputPanel.setOutput(output);
}
}
}
class OutputPanel extends JPanel {
private static JTextArea outputArea;
public OutputPanel() {
outputArea = new JTextArea(10, 30);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(scrollPane);
}
public static void setOutput(String output) {
outputArea.setText(output);
}
}