Repository navigation
Expand file tree
/
Copy pathCryptogravisor.java
More file actions
284 lines (235 loc) · 8.65 KB
/
Copy pathCryptogravisor.java
File metadata and controls
284 lines (235 loc) · 8.65 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.math.BigInteger;
import java.security.SecureRandom;
public class Cryptogravisor extends JFrame implements ActionListener{
//the main class of the program
//handles all the GUI, and calls methods in other classes
private JPanel panelSouth, panelWest, panelEast, panelCenter, panelNorth;
private JButton send, addContact, removeContact;//, decrypt;
private JTextField input;
private JTextArea convo;
private JLabel labelmessage;
private JList JLcontacts;
private ArrayList<Encryption> encryptlist;
private Contacts contacts;
private Communications comm;
private DefaultListModel tmplist;
public Cryptogravisor() {
//initialize lists
encryptlist=new ArrayList<Encryption>();
contacts=new Contacts();
//initialize GUI and stuff
comm=new Communications(6000,6001,this);
setVisible(true);
//initialize GUI
panelSouth = new JPanel();
panelEast = new JPanel();
panelWest = new JPanel();
panelCenter = new JPanel();
panelNorth = new JPanel();
//create south panel
panelSouth.setLayout(new FlowLayout());
labelmessage = new JLabel("Enter a message:");
panelSouth.add(labelmessage);
input = new JTextField(20);
input.addActionListener(this);
panelSouth.add(input);
send = new JButton("Send");
panelSouth.add(send);
send.addActionListener(this);
//decrypt = new JButton ("Decrypt Message");
//panelSouth.add(decrypt);
//create center panel
panelCenter.setLayout(new FlowLayout());
convo = new JTextArea("New Convo\n", 30, 35);
convo.setLineWrap(true);
convo.setWrapStyleWord(true);
convo.setEditable(false);
panelCenter.add(convo);
//create west panel
panelWest.setLayout(new BorderLayout());
addContact = new JButton ("Add Contact");
panelWest.add(addContact, BorderLayout.NORTH);
addContact.addActionListener(this);
tmplist=new DefaultListModel();
JLcontacts = new JList(tmplist);
JLcontacts.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//contacts.addActionListener(this);
panelWest.add(JLcontacts, BorderLayout.CENTER);
removeContact = new JButton ("Remove Contact");
removeContact.addActionListener(this);
panelWest.add(removeContact, BorderLayout.SOUTH);
add(panelSouth, BorderLayout.SOUTH);
add(panelCenter, BorderLayout.CENTER);
add(panelWest, BorderLayout.WEST);
setTitle("Cryptography Messenger");
setSize(655,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
//responds to user input
//delegates message sends, and user adds
if(e.getSource()==send||e.getSource()==input){
String newMessage = input.getText();
try {
if( newMessage.equals("")) {
//notify user they should enter a message
JOptionPane.showMessageDialog(null, "Please enter a message");
}
else if (JLcontacts.getSelectedIndex()==-1) {
//notify user to select a contact
JOptionPane.showMessageDialog(null, "Please select a contact");
}
else {
sendMSG(contacts.getAddress(JLcontacts.getSelectedIndex()),newMessage);
}
}
catch (Exception ex) {
System.out.println("No key existing yet!");
}
input.setText("");
}
else if(e.getSource()==addContact){
//prompts user to input a new name and IP
String name = JOptionPane.showInputDialog(null, "Enter a name:");
String IP = JOptionPane.showInputDialog(null, "Enter the IP:");
addToContacts(name,IP);
}
else if(e.getSource()==removeContact){
int ind = JLcontacts.getSelectedIndex();
try {
tmplist.remove(ind);
contacts.removeContact(ind);
encryptlist.remove(ind);
JLcontacts.remove(ind);
JLcontacts.setSelectedIndex(0);
} catch (Exception e1){
e1.printStackTrace();
JOptionPane.showMessageDialog(null,"An error occured erasing the contact");
}
}
}
private void addToContacts(String n, String address) {
//checks if IP already in use
if (contacts.containsAddress(address)) {
//remove contact
int l=contacts.indexOfAddress(address);
contacts.removeContact(l);
encryptlist.remove(l);
tmplist.remove(l);
}
byte[] bigint = Encryption.calcMod();
// adds the user entry to the contacts class
contacts.addContact(n, address);
encryptlist.add(new Encryption(bigint));
// sends the key and mod to given address
int ind=contacts.indexOfAddress(address);
try {
System.out.println(bigint.length+" : "+encryptlist.get(ind).DiffieHellmanComputeKey().length);
comm.sendKeyAndMod(address, encryptlist.get(ind).DiffieHellmanComputeKey(), bigint);
tmplist.addElement(n+"@"+address);
JLcontacts.setSelectedIndex(ind);
}
catch (Exception e) {
//give dialog
JOptionPane.showMessageDialog(null, "An error occured connecting to the other user!");
System.out.println("ERROR");
e.printStackTrace();
//something went wrong connecting, remove user from list
encryptlist.remove(ind);
contacts.removeContact(ind);
JLcontacts.setSelectedIndex(0);
}
}
private void sendMSG(String address, String message) throws InvalidatedEncryptionException {
//sends messaged based on the user's selected contact and their message in the textfield
//encrypts for recipient, and sends to user with communications class
// sends the encrypted message to the address
convo.append("You: "+message+"\n");
if (contacts.containsAddress(address)) {
String ret=encryptlist.get(contacts.indexOfAddress(address)).encryptText(message);
try {
comm.send(address, ret);
}
catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "An error occured sending message to other user");
}
convo.append("You(Encrypted)->"+address+": "+ret+"\n");
}
}
public void handleMessage(String message, String address) throws InvalidatedEncryptionException {
if (!contacts.containsAddress(address)) {
convo.append("UNKNOWN COMMUNICATOR: "+message+"\n");
}
//called by server, and will be printed to GUI by this method
// Creates the variables to find the correct encryption address
else {
int index = contacts.indexOfAddress(address);
// When message is received it will be encrypted prints out encrypted message for testing purposes
// Decrypts the message and prints it
// Assumes all messages that are received are encrypted
convo.append(contacts.getName(index)+"@"+contacts.getAddress(index)+"(Encrypted): "+message+"\n");
convo.append(contacts.getName(index)+"@"+contacts.getAddress(index)+": "+encryptlist.get(index).decryptText(message)+"\n");
}
}
public void handleKey(byte[] info, String address) {
//handles encryption info passed for public key crypt
System.out.println("KEY INBOUND");
if (contacts.containsAddress(address)) {
System.out.println("Key recieved");
int ind=contacts.indexOfAddress(address);
encryptlist.get(ind).calcKey(info);
}
}
public void quit() {
//ends program, sever connections
}
public void handleKeyAndMod(byte dat[], byte mod[], String address){
// Adds a new friend contact for the user to change the name of
System.out.println("rec mod"+address);
//check to see if ip is already in list
if (contacts.containsAddress(address)){
final int l=contacts.indexOfAddress(address);
contacts.removeContact(l);
encryptlist.remove(l);
System.out.println("ind "+l);
//wait for other thread to catch up
SwingUtilities.invokeLater(new Runnable() {
public void run() {
tmplist.remove(l);
}
});
}
contacts.addContact("New Friend", address);
encryptlist.add(new Encryption(mod));
// Calculates the key
int ind=contacts.indexOfAddress(address);
encryptlist.get(ind).calcKey(dat);
try {
comm.sendKey(address,encryptlist.get(ind).DiffieHellmanComputeKey());
tmplist.addElement("New Friend@"+address);
//JLcontacts.setSelectedIndex(ind);
//we don't change the user, so we don't distrupt presumed messaging with a different user
}
catch (Exception e) {
System.out.println("ERROR");
//give error message
convo.append("An error occurred handling connection from "+address+"\n");
e.printStackTrace();
//something went wrong connecting, remove user from list
encryptlist.remove(ind);
contacts.removeContact(ind);
JLcontacts.setSelectedIndex(0);
}
}
public static void main(String args[]) {
new Cryptogravisor();
}
}