-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGUI.java
177 lines (144 loc) · 5.11 KB
/
GUI.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
package com.encryption;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class GUI {
Font font;
JButton Openbutton,encryptButton,decryptButton;
JComboBox<String> dropDown;
File file;
byte fileBytes[],resultBytes[];
JTextField textField;
void createDropdown(JFrame frame){
String[] optionsToChoose = {"AES", "XOR", "TDES"};
dropDown = new JComboBox<>(optionsToChoose);
dropDown.setBounds(80, 50, 140, 20);
frame.add(dropDown);
}
void createFrame(JFrame frame){
frame.setTitle("Encryption"); //set the title of the frame
frame.setSize(700,700); //set the size of the frame
frame.setLocationRelativeTo(null); //set the location of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the default close operation of the frame
frame.setVisible(true); //set the frame to visible
}
void createButton(JFrame frame){
font= new Font("roboto", Font.BOLD, 20);
frame.setLayout(new FlowLayout());
// creating open file button
Openbutton = new JButton("Open image");
Openbutton.setFont(font);
Openbutton.setBounds(100,10,70,50);
Openbutton.setSize(10,10);
// creating text field
textField = new JTextField(10);
textField.setFont(font);
textField.setBounds(150,10,100,50);
//ActionListner to get file
Openbutton.addActionListener(e->{
getFile();
});
encryptButton=new JButton("Encrypt");
encryptButton.setFont(font);
encryptButton.setBounds(130,100,80,50);
encryptButton.addActionListener(e -> {
getFileData();
String privateKey=textField.getText();
String selectedAlgo =dropDown.getItemAt(dropDown.getSelectedIndex());
if(selectedAlgo.equals("XOR")) {
int key=0;
try {
key=Integer.parseInt(privateKey);
}catch (Exception exception)
{
exception.printStackTrace();
}
resultBytes = XOR.operateXOR(key, fileBytes);
}
if(selectedAlgo.equals("AES")){
try {
resultBytes=AES.encryptData(privateKey,fileBytes);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
if(selectedAlgo.equals("TDES"))
{
try {
resultBytes=TDES.encrypt(fileBytes);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
storeResultData("Encrypted");
});
decryptButton=new JButton("Decrypt");
decryptButton.setFont(font);
decryptButton.setBounds(130,100,80,50);
decryptButton.addActionListener(e -> {
getFileData();
String privateKey=textField.getText();
String selectedAlgo =dropDown.getItemAt(dropDown.getSelectedIndex());
if(selectedAlgo.equals("XOR")) {
int key=0;
try{
key=Integer.parseInt(privateKey);
}
catch (Exception exception) {
exception.printStackTrace();
resultBytes = XOR.operateXOR(key, fileBytes);
}
}
if(selectedAlgo.equals("AES")){
try {
resultBytes=AES.decryptData(privateKey,fileBytes);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
if(selectedAlgo.equals("TDES"))
{
try {
resultBytes=TDES.decrypt(fileBytes);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
storeResultData("Decrytped");
});
frame.add(Openbutton);
frame.add(textField);
frame.add(encryptButton);
frame.add(decryptButton);
}
private void getFileData() {
try {
FileInputStream fis=new FileInputStream(file);
fileBytes=new byte[fis.available()];
System.out.println(fileBytes.length);
fis.read(fileBytes);
fis.close();
}catch (Exception e){
e.printStackTrace();
}
}
private void storeResultData(String message)
{
try {
FileOutputStream fileOutputStream=new FileOutputStream(file);
fileOutputStream.write(resultBytes);
fileOutputStream.close();
JOptionPane.showMessageDialog(null,message);
}catch (Exception e){
e.printStackTrace();
}
}
private void getFile() {
JFileChooser fileChooser =new JFileChooser();
fileChooser.showOpenDialog(null);
file =fileChooser.getSelectedFile();
JOptionPane.showMessageDialog(null,"File chosen : "+file.getName());
}
}