-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
211 lines (192 loc) · 8.2 KB
/
Main.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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
class CipherAppSwing {
public static void main(String[] args) {
// Membuat JFrame
JFrame frame = new JFrame("Cipher Encryptor Decryptor Java");
frame.setSize(1512, 982);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
// Membuat Panel Utama
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
frame.add(panel, BorderLayout.CENTER);
placeComponents(panel);
panel.setBackground(Color.black);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(10, 10, 10, 10); // Padding
// Judul
JLabel titleLabel = new JLabel("Cipher Encryptor Decryptor Java");
titleLabel.setFont(new Font("Arial", Font.BOLD, 30));
titleLabel.setForeground(Color.RED);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 3;
constraints.anchor = GridBagConstraints.CENTER;
panel.add(titleLabel, constraints);
// Separator
JSeparator separator = new JSeparator();
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 3;
constraints.fill = GridBagConstraints.HORIZONTAL;
panel.add(separator, constraints);
// Label untuk input pesan
JLabel messageLabel = new JLabel("Message:");
constraints.gridwidth = 1;
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(messageLabel, constraints);
messageLabel.setForeground(Color.RED);
// Text field untuk input pesan
JTextField messageText = new JTextField(15);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(messageText, constraints);
messageText.setForeground(Color.RED);
// Tombol untuk mengunggah file .txt
JButton uploadButton = new JButton("Upload .txt File");
constraints.gridx = 2;
constraints.gridy = 2;
panel.add(uploadButton, constraints);
uploadButton.setForeground(Color.RED);
// Label untuk input kunci
JLabel keyLabel = new JLabel("Key (min. 12 chars):");
constraints.gridx = 0;
constraints.gridy = 3;
panel.add(keyLabel, constraints);
keyLabel.setForeground(Color.RED);
// Text field untuk input kunci
JTextField keyText = new JTextField(15);
constraints.gridx = 1;
constraints.gridy = 3;
panel.add(keyText, constraints);
keyText.setForeground(Color.RED);
// ComboBox untuk memilih cipher
JLabel cipherLabel = new JLabel("Select Cipher:");
constraints.gridx = 0;
constraints.gridy = 4;
panel.add(cipherLabel, constraints);
cipherLabel.setForeground(Color.RED);
String[] ciphers = {"Vigenere Cipher", "Playfair Cipher", "Hill Cipher"};
JComboBox<String> cipherComboBox = new JComboBox<>(ciphers);
constraints.gridx = 1;
constraints.gridy = 4;
panel.add(cipherComboBox, constraints);
cipherComboBox.setForeground(Color.RED);
// Tombol untuk enkripsi
JButton encryptButton = new JButton("Encrypt");
constraints.gridx = 0;
constraints.gridy = 5;
panel.add(encryptButton, constraints);
encryptButton.setForeground(Color.RED);
// Tombol untuk dekripsi
JButton decryptButton = new JButton("Decrypt");
constraints.gridx = 1;
constraints.gridy = 5;
panel.add(decryptButton, constraints);
decryptButton.setForeground(Color.RED);
// Label untuk hasil
JLabel resultLabel = new JLabel("Result:");
constraints.gridx = 0;
constraints.gridy = 6;
panel.add(resultLabel, constraints);
resultLabel.setForeground(Color.RED);
// Text area untuk menampilkan hasil
JTextArea resultArea = new JTextArea(5, 15);
resultArea.setLineWrap(true);
resultArea.setWrapStyleWord(true);
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
constraints.gridx = 1;
constraints.gridy = 6;
constraints.gridwidth = 2;
panel.add(scrollPane, constraints);
resultArea.setForeground(Color.RED);
// Action untuk tombol Upload
uploadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
messageText.setText(stringBuilder.toString().trim());
} catch (IOException ex) {
ex.printStackTrace();
}
}
messageText.setForeground(Color.RED);
}
});
// Action untuk tombol Encrypt
encryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = messageText.getText();
String key = keyText.getText();
String selectedCipher = (String) cipherComboBox.getSelectedItem();
if (key.length() >= 12) {
String encryptedMessage = "";
switch (selectedCipher) {
case "Vigenere Cipher":
encryptedMessage = CipherUtils.vigenereEncrypt(message, key);
break;
case "Playfair Cipher":
encryptedMessage = CipherUtils.playfairEncrypt(message, key);
break;
case "Hill Cipher":
encryptedMessage = CipherUtils.hillEncrypt(message, key);
break;
}
resultArea.setText(encryptedMessage);
} else {
resultArea.setText("Key must be at least 12 characters");
}
}
});
// Action untuk tombol Decrypt
decryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = messageText.getText();
String key = keyText.getText();
String selectedCipher = (String) cipherComboBox.getSelectedItem();
if (key.length() >= 12) {
String decryptedMessage = "";
switch (selectedCipher) {
case "Vigenere Cipher":
decryptedMessage = CipherUtils.vigenereDecrypt(message, key);
break;
case "Playfair Cipher":
decryptedMessage = CipherUtils.playfairDecrypt(message, key);
break;
case "Hill Cipher":
decryptedMessage = CipherUtils.hillDecrypt(message, key);
break;
}
resultArea.setText(decryptedMessage);
} else {
resultArea.setText("Key must be at least 12 characters");
}
}
});
}
}