-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipherGUI.java
More file actions
334 lines (261 loc) · 8.78 KB
/
CipherGUI.java
File metadata and controls
334 lines (261 loc) · 8.78 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
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class CipherGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new CipherGUI());
}
private final CipherModel model;
private CipherPanel encryptPanel;
private CipherPanel decryptPanel;
public CipherGUI() {
this.model = new CipherModel();
}
@Override
public void run() {
JFrame frame = new JFrame("Cipher GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
System.out.println(frame.getSize());
}
private JTabbedPane createMainPanel() {
final JTabbedPane tabbedPane = new JTabbedPane();
Font font = tabbedPane.getFont().deriveFont(16f);
tabbedPane.setFont(font);
String plainTextTitle = "Plain Text";
String encryptedTitle = "Cipher Text";
encryptPanel = new CipherPanel(this, model, model.getEncryptTitle(),
plainTextTitle, encryptedTitle);
tabbedPane.add("Encode Text", encryptPanel.getPanel());
decryptPanel = new CipherPanel(this, model, model.getDecryptTitle(),
encryptedTitle, plainTextTitle);
tabbedPane.add("Decode Text", decryptPanel.getPanel());
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent event) {
if (tabbedPane.getSelectedIndex() == 0) {
encryptPanel.setKeyPhrase(model.getKeyPhrase());
encryptPanel.setInputText(model.getDecryptedText());
}
if (tabbedPane.getSelectedIndex() == 1) {
decryptPanel.setKeyPhrase(model.getKeyPhrase());
decryptPanel.setInputText(model.getEncryptedText());
}
}
});
return tabbedPane;
}
public CipherPanel getEncryptPanel() {
return encryptPanel;
}
public CipherPanel getDecryptPanel() {
return decryptPanel;
}
public class CipherPanel {
private final CipherGUI frame;
private final CipherModel model;
private final JPanel panel;
private JTextArea inputArea;
private JTextArea outputArea;
private JTextField keyPhraseField;
private final String buttonText;
private final String inputTitle;
private final String outputTitle;
public CipherPanel(CipherGUI frame, CipherModel model,
String buttonText, String inputTitle, String outputTitle) {
this.frame = frame;
this.model = model;
this.buttonText = buttonText;
this.inputTitle = inputTitle;
this.outputTitle = outputTitle;
this.panel = createMainPanel();
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(16f);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel keyPhraseLabel = new JLabel("Key Phrase");
keyPhraseLabel.setFont(font);
panel.add(keyPhraseLabel, gbc);
gbc.gridy++;
keyPhraseField = new JTextField(50);
keyPhraseField.setFont(font);
panel.add(keyPhraseField, gbc);
gbc.gridy++;
JLabel inputLabel = new JLabel(inputTitle);
inputLabel.setFont(font);
panel.add(inputLabel, gbc);
gbc.gridy++;
inputArea = new JTextArea(10, 50);
inputArea.setFont(font);
inputArea.setLineWrap(true);
inputArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(inputArea);
panel.add(scrollPane, gbc);
gbc.gridy++;
JButton button = new JButton(buttonText);
button.addActionListener(new ButtonListener(frame, model));
button.setFont(font);
panel.add(button, gbc);
gbc.gridy++;
JLabel outputLabel = new JLabel(outputTitle);
outputLabel.setFont(font);
panel.add(outputLabel, gbc);
gbc.gridy++;
outputArea = new JTextArea(10, 50);
outputArea.setEditable(false);
outputArea.setFont(font);
outputArea.setLineWrap(true);
outputArea.setWrapStyleWord(true);
scrollPane = new JScrollPane(outputArea);
panel.add(scrollPane, gbc);
return panel;
}
public JPanel getPanel() {
return panel;
}
public void setKeyPhrase(String keyPhrase) {
this.keyPhraseField.setText(keyPhrase);
}
public String getKeyPhrase() {
return keyPhraseField.getText().trim();
}
public void setInputText(String inputText) {
this.inputArea.setText(inputText);
}
public String getInputText() {
return inputArea.getText().trim();
}
public void setOutputText(String text) {
this.outputArea.setText(text);
}
}
public class ButtonListener implements ActionListener {
private final CipherGUI frame;
private final CipherModel model;
public ButtonListener(CipherGUI frame, CipherModel model) {
this.frame = frame;
this.model = model;
}
@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
String buttonText = button.getText();
if (buttonText.equals(model.getDecryptTitle())) {
String keyPhrase = frame.getDecryptPanel().getKeyPhrase();
String inputText = frame.getDecryptPanel().getInputText();
if (keyPhrase.isEmpty() || inputText.isEmpty()) {
return;
}
model.setKeyPhrase(keyPhrase);
model.setEncryptedText(inputText);
model.decryptText();
frame.getDecryptPanel().setOutputText(model.getDecryptedText());
}
if (buttonText.equals(model.getEncryptTitle())) {
String keyPhrase = frame.getEncryptPanel().getKeyPhrase();
String inputText = frame.getEncryptPanel().getInputText();
if (keyPhrase.isEmpty() || inputText.isEmpty()) {
return;
}
model.setKeyPhrase(keyPhrase);
model.setDecryptedText(inputText);
model.encryptText();
frame.getEncryptPanel().setOutputText(model.getEncryptedText());
}
}
}
public class CipherModel {
private final String encryptTitle;
private final String decryptTitle;
private String keyPhrase;
private String decryptedText;
private String encryptedText;
public CipherModel() {
this.encryptTitle = "Encrypt Text";
this.decryptTitle = "Decrypt Text";
}
public String getKeyPhrase() {
return keyPhrase;
}
public void setKeyPhrase(String keyPhrase) {
this.keyPhrase = keyPhrase;
}
public String getDecryptedText() {
return decryptedText;
}
public void setDecryptedText(String decryptedText) {
this.decryptedText = decryptedText;
}
public String getEncryptedText() {
return encryptedText;
}
public void setEncryptedText(String encryptedText) {
this.encryptedText = encryptedText;
}
public void decryptText() {
StringBuilder builder = new StringBuilder();
int keyIndex = 0;
int textIndex = 0;
// ASCII characters go from 32 to 126
while (textIndex < encryptedText.length()) {
int shift = (int) keyPhrase.charAt(keyIndex++) - 31;
char c = encryptedText.charAt(textIndex++);
int value = ((int) c - shift);
value = (value < 32) ? value + 95 : value;
char d = (char) value;
builder.append(d);
keyIndex %= keyPhrase.length();
}
decryptedText = builder.toString();
}
public void encryptText() {
StringBuilder builder = new StringBuilder();
int keyIndex = 0;
int textIndex = 0;
// ASCII characters go from 32 to 126
while (textIndex < decryptedText.length()) {
int shift = (int) keyPhrase.charAt(keyIndex++) - 31;
char c = decryptedText.charAt(textIndex++);
int value = (int) c + shift;
value = (value > 126) ? value - 95 : value;
char d = (char) value;
builder.append(d);
keyIndex %= keyPhrase.length();
}
encryptedText = builder.toString();
}
public String getEncryptTitle() {
return encryptTitle;
}
public String getDecryptTitle() {
return decryptTitle;
}
}
}