-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
314 lines (228 loc) · 8.13 KB
/
ChatClient.java
File metadata and controls
314 lines (228 loc) · 8.13 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
// ChatClient.java
//
// Modified 1/30/2000 by Alan Frindell
// Last modified 2/18/2003 by Ting Zhang
// Last modified : Priyank Patel <pkpatel@cs.stanford.edu>
//
// Chat Client starter application.
// AWT/Swing
import java.awt.*;
import java.awt.event.*;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.swing.*;
// Java
import java.io.*;
// socket
import java.net.*;
// Crypto
import java.security.*;
import java.util.Base64;
public class ChatClient {
public static final int SUCCESS = 0;
public static final int CONNECTION_REFUSED = 1;
public static final int BAD_HOST = 2;
public static final int ERROR = 3;
String _loginName;
ChatServer _server;
ChatClientThread _thread;
ChatLoginPanel _loginPanel;
ChatRoomPanel _chatPanel;
ChatRoomSelectPanel _chatRoomSelectPanel;
PrintWriter _out = null;
BufferedReader _in = null;
CardLayout _layout;
JFrame _appFrame;
private String chatRoomName;
private SecretKey sessionKey;
private int _clientId;
private byte[] iv;
public String getChatRoomName() {
return chatRoomName;
}
Socket _socket = null;
SecureRandom secureRandom;
KeyStore clientKeyStore;
KeyStore caKeyStore;
// KeyManagerFactory keyManagerFactory;
// TrustManagerFactory trustManagerFactory;
// ChatClient Constructor
//
// empty, as you can see.
public ChatClient() {
_loginName = null;
_server = null;
//todo yanlissa update yaparsin iv lere.
iv = EncryptionUtil.generateIv();
try {
initComponents();
} catch (Exception e) {
System.out.println("ChatClient error: " + e.getMessage());
e.printStackTrace();
}
_layout.show(_appFrame.getContentPane(), "Login");
}
public void run() {
_appFrame.pack();
_appFrame.setVisible(true);
}
// main
//
// Construct the app inside a frame, in the center of the screen
public static void main(String[] args) {
ChatClient app = new ChatClient();
app.run();
}
// initComponents
//
// Component initialization
private void initComponents() throws Exception {
_appFrame = new JFrame("BIL448 Chat");
_layout = new CardLayout();
_appFrame.getContentPane().setLayout(_layout);
_loginPanel = new ChatLoginPanel(this);
_chatPanel = new ChatRoomPanel(this);
_chatRoomSelectPanel = new ChatRoomSelectPanel(this);
_appFrame.getContentPane().add(_loginPanel, "Login");
_appFrame.getContentPane().add(_chatPanel, "ChatRoom");
_appFrame.getContentPane().add(_chatRoomSelectPanel,"ChatRoomSelect");
_appFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
quit();
}
});
}
// quit
//
// Called when the application is about to quit.
public void quit() {
try {
_socket.shutdownOutput();
_thread.join();
_socket.close();
} catch (Exception err) {
System.out.println("ChatClient error: " + err.getMessage());
err.printStackTrace();
}
System.exit(0);
}
//
// connect
//
// Called from the login panel when the user clicks the "connect"
// button. You will need to modify this method to add certificate
// authentication.
// There are two passwords : the keystorepassword is the password
// to access your private key on the file system
// The other is your authentication password on the CA.
//
public int connect(String loginName, char[] password,
String keyStoreName, char[] keyStorePassword,
String caHost, int caPort,
String serverHost, int serverPort) {
try {
_loginName = loginName;
//
// Read the client keystore
// (for its private/public keys)
// Establish secure connection to the CA
// Send public key and get back certificate
// Use certificate to establish secure connection with server
//
_socket = new Socket(serverHost, serverPort);
_out = new PrintWriter(_socket.getOutputStream(), true);
_in = new BufferedReader(new InputStreamReader(
_socket.getInputStream()));
_layout.show(_appFrame.getContentPane(),"ChatRoomSelect");
_thread = new ChatClientThread(this);
_thread.start();
//todo burayi silersin ilk kismi implement ettikten sonra. Session key creation ve transfer icin.
/*_out.println(CommunicationType.KEY);
_out.println(keyStr);*/
return SUCCESS;
} catch (UnknownHostException e) {
System.err.println("Don't know about the serverHost: " + serverHost);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to the serverHost: " + serverHost);
System.out.println("ChatClient error: " + e.getMessage());
e.printStackTrace();
System.exit(1);
} catch (AccessControlException e) {
return BAD_HOST;
} catch (Exception e) {
System.out.println("ChatClient err: " + e.getMessage());
e.printStackTrace();
}
return ERROR;
}
public void enterChatRoom(){
_layout.show(_appFrame.getContentPane(), "ChatRoom");
}
//todo once bu metodu cagirmak mantikli olabilir.
public void joinChatRoom(String chatRoomName) {
this.chatRoomName = chatRoomName;
//todo şimdilik loginName veriliyo clientID olarak. Gerekiyorsa degistir.
String encrypted1 = null;
String mac1 = null;
try {
encrypted1 = EncryptionUtil.encrypt(this.chatRoomName,this.sessionKey,iv);
mac1 = EncryptionUtil.getMac(this.sessionKey,""+encrypted1+this._loginName);
} catch (NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
_out.println(CommunicationType.CHATROOM_JOIN.toString());
_out.println(Base64.getEncoder().encodeToString(iv));
_out.println(this._loginName);
_out.println(encrypted1);
_out.println(mac1);
}
// sendMessage
//
// Called from the ChatPanel when the user types a carrige return.
public void sendMessage(String msg) {
try {
//todo 1 blocka sigmayan mesajari da şifreleyip gonderme eklenecek.
msg = _loginName + "> " + msg;
String plainText = msg+ this.chatRoomName;
SecretKey key = EncryptionUtil.generateKey(128);
String cipherText = EncryptionUtil.encrypt(plainText,key,iv);
String macResultString = EncryptionUtil.getMac(key, cipherText);
sendMessagesToServer(this.chatRoomName,cipherText,macResultString);
} catch (Exception e) {
System.out.println("ChatClient err: " + e.getMessage());
e.printStackTrace();
}
}
public Socket getSocket() {
return _socket;
}
public JTextArea getOutputArea() {
return _chatPanel.getOutputArea();
}
private void sendMessagesToServer(String chatRoomId, String cipherText, String mac){
_out.println(CommunicationType.MSG_SEND.toString());
_out.println(chatRoomId);
_out.println(cipherText);
_out.println(mac);
}
public SecretKey getSessionKey() {
return sessionKey;
}
public void setSessionKey(SecretKey sessionKey) {
this.sessionKey = sessionKey;
}
public int get_clientId() {
return _clientId;
}
public void set_clientId(int _clientId) {
this._clientId = _clientId;
}
public byte[] getIv() {
return iv;
}
public void setIv(byte[] iv) {
this.iv = iv;
}
}