- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureChatClient.java
More file actions
Latest commit
209 lines (178 loc) · 6.86 KB
/
Copy pathSecureChatClient.java
File metadata and controls
209 lines (178 loc) · 6.86 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
importjava.util.*;
importjava.io.*;
importjava.math.BigInteger;
importjava.net.*;
importjavax.swing.*;
importjavax.swing.text.DefaultCaret;
importjava.awt.event.*;
importjava.awt.*;
/* TODO: Extra Credit features
* Changing your chat color
* Changing your window color
* List of active users - might not be possible without altering the server file
*
*/
@SuppressWarnings("serial")
publicclassSecureChatClientextendsJFrameimplementsRunnable, ActionListener {
publicstaticfinalintPORT = 8765;
//GUI Variables
JTextAreaoutputArea;
JLabelprompt;
JTextFieldinputField;
StringmyName, serverName;
//Other Variables
Socketconnection; // Connection to the server
BigIntegerE, N, IntKey; // BigIntegers needed to perform the RSA encryption
ObjectInputStreamobjReader; // Reads objects from the server
ObjectOutputStreamobjWriter; // Sends objects to the server
SymCiphercipher; // Interface reference to hold either the Add128 or Substitute cipher
StringencType; // String representation of the encryption cipher the server chose
staticintexitCode = 0;
publicSecureChatClient()
{
try {
// Get user/server information and setup the server connection
myName = JOptionPane.showInputDialog(this, "Enter your user name: ");
serverName = JOptionPane.showInputDialog(this, "Enter the server name: ");
InetAddressaddr = InetAddress.getByName(serverName);
connection = newSocket(addr, PORT); // Connect to server with new Socket
// Create the I/O Streams to communicate with the server
objWriter = newObjectOutputStream(connection.getOutputStream());
objWriter.flush(); //Prevent deadlock
objReader = newObjectInputStream(connection.getInputStream());
// Get the servers public key and cipher configuration
E = (BigInteger) objReader.readObject();
N = (BigInteger) objReader.readObject();
encType = (String) objReader.readObject();
// Initialize the cipher and request the key in the form of a byte[]
cipher = encType.equals("Add") ? newAdd128() : newSubstitute();
byte[] key = cipher.getKey();
IntKey = newBigInteger(1, key); // Construct a BigInteger representation of the key
BigIntegerencKey = IntKey.modPow(E, N); // Encrypt the BigInt key using the servers public key
// Send my encrypted key to the server since each new user (instantiation of this class) will have their
// own unique cipher, server uses them to re/de-encrypt messages to each user individually
objWriter.writeObject(encKey);
//Print client side values to console
System.out.println("My E: " + E);
System.out.println("My N: " + N);
System.out.println("Encryption type: " + cipher.getClass());
System.out.println("Symmetric Key: ");
for(byteb: key)
System.out.print(b + " ");
System.out.println();
// Send encrypted name to the server, needed to announce sign-on and sign-off
objWriter.writeObject(cipher.encode(myName));
this.setTitle(myName); // Set title to identify chatter's window
// Set up the TestField for user input
inputField = newJTextField("");
inputField.setBounds(0, 352, 500, 26);
inputField.addActionListener(this);
prompt = newJLabel("Type your messages below:");
prompt.setBounds(0, 333, 500, 20);
Containerc = getContentPane();
c.setLayout(null);
c.add(prompt);
c.add(inputField);
// Set up the TextArea to display the message thread
outputArea = newJTextArea(8, 20);
outputArea.setBackground(Color.WHITE);
outputArea.setEditable(false);
outputArea.append(" Welcome to the Chat Group, " + myName + "\n");
// Make the TextArea into a ScrollPane that auto scrolls as news messages are received
JScrollPanescrollPane = newJScrollPane();
scrollPane.setBounds(6, 25, 488, 296);
c.add(scrollPane);
scrollPane.setViewportView(outputArea);
DefaultCaretcaret = (DefaultCaret)outputArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
// Top MenuBar that hold the individual menues
JMenuBarmenuBar = newJMenuBar();
menuBar.setBounds(0, 0, 494, 22);
getContentPane().add(menuBar);
// Menu drop down menus for the Text Color and Background Color
JMenutextMenu = newJMenu("Text Color");
JMenubackMenu = newJMenu("Background Color");
menuBar.add(textMenu);
menuBar.add(backMenu);
// Create the Text Menu options
JMenuItemtextBlack = newJMenuItem("Black");
JMenuItemtextWhite = newJMenuItem("White");
JMenuItemtextGreen = newJMenuItem("Green");
JMenuItembackWhite = newJMenuItem("White");
JMenuItembackBlack = newJMenuItem("Black");
// Add the options to the Menu
textMenu.add(textBlack);
textMenu.add(textWhite);
textMenu.add(textGreen);
backMenu.add(backWhite);
backMenu.add(backBlack);
// Lambda functions act as simplified ActionListener to give buttons functionality
textBlack.addActionListener(e -> {outputArea.setForeground(Color.BLACK);});
textWhite.addActionListener(e -> {outputArea.setForeground(Color.WHITE);});
textGreen.addActionListener(e -> {outputArea.setForeground(Color.GREEN);});
backWhite.addActionListener(e -> {outputArea.setBackground(Color.WHITE);});
backBlack.addActionListener(e -> {outputArea.setBackground(Color.BLACK);});
ThreadoutputThread = newThread(this); // Thread is to receive strings
outputThread.start(); // from Server
addWindowListener( newWindowAdapter() {
publicvoidwindowClosing(WindowEvente)
{
try
{
objWriter.writeObject(cipher.encode("CLIENT CLOSING"));
}
catch (IOExceptionex)
{
System.out.println("Error closing client!");
exitCode = 1;
}
finally
{
System.exit(exitCode);
}
}
} );
setSize(500, 400);
setVisible(true);
}
catch (Exceptione)
{
System.out.println("Problem starting client!");
}
}
publicvoidrun()
{
while (true)
{
try
{
byte[] curMsg = (byte[]) objReader.readObject(); // Read encrypted msg from the server
outputArea.append(' ' + cipher.decode(curMsg) + "\n"); // Decrypt the message and display it on the GUI
}
catch (Exceptione)
{
System.out.println(e + ", closing client!");
break;
}
}
System.exit(0);
}
publicvoidactionPerformed(ActionEvente)
{
StringcurMsg = e.getActionCommand(); // Get inputField value from the GUI
inputField.setText(""); // Clear the inputField
try
{
objWriter.writeObject(cipher.encode(myName + ": " + curMsg)); //Encrypt the msg and send it to the server
}
catch (Exceptionex)
{
System.out.println("Error Encrypting Message");
}
}
publicstaticvoidmain(String [] args)
{
SecureChatClientJR = newSecureChatClient();
JR.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // Must trigger WindowListener to signoff from server
}
}