-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServerThread.java
More file actions
159 lines (112 loc) · 4.62 KB
/
ChatServerThread.java
File metadata and controls
159 lines (112 loc) · 4.62 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
//
// ChatServerThread.java
// created 02/18/03 by Ting Zhang
// Modified : Priyank K. Patel <pkpatel@cs.stanford.edu>
//
// Java
import java.util.*;
import java.math.BigInteger;
// socket
import java.net.*;
import java.io.*;
// Crypto
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
public class ChatServerThread extends Thread {
private Socket _socket = null;
private ChatServer _server = null;
private Hashtable _records = null;
private Mac mac;
private int clientId;
private PrintWriter outClient;
private ClientRecord clientRecord;
public ChatServerThread(ChatServer server, Socket socket,int clientId) {
super("ChatServerThread");
_server = server;
_socket = socket;
_records = server.getClientRecords();
this.clientId = clientId;
}
public void run() {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
_socket.getInputStream()));
clientRecord = (ClientRecord) _records.get(clientId);
openOutStreamToClient();
sendChatroomKeyToClient();
String receivedMsg;
while ((receivedMsg = in.readLine()) != null ) {
if (receivedMsg.equals(CommunicationType.MSG_SEND.toString())){
String chatRoomId = in.readLine();
String cipherText = in.readLine();
String macText = in.readLine();
Mac mac = Mac.getInstance("HmacSHA256");
Enumeration theClients = _records.elements();
while (theClients.hasMoreElements()) {
ClientRecord c = (ClientRecord) theClients.nextElement();
if (c.getChatRoomId().equals(chatRoomId)){
Socket socket = c.getClientSocket();
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(cipherText);
}else{
//Client A roomunda degilmis ona mesaj gitmez.
}
}
}
else if (receivedMsg.equals(CommunicationType.KEY.toString())){
String key = in.readLine();
}
else if(receivedMsg.equals(CommunicationType.CHATROOM_JOIN.toString())){
String iv = in.readLine();
String clientId = in.readLine();
String encrypted1 = in.readLine();
String mac1 = in.readLine();
byte[] ivClient = Base64.getDecoder().decode(iv);
if (checkMac(clientId,encrypted1,mac1)){
//send the chatroom key..
System.out.println("Mac is true..");
String chatRoomId = EncryptionUtil.decrypt(encrypted1,clientRecord.getSecretKey(),ivClient);
clientRecord.setChatRoomId(chatRoomId);
}
}
}
_socket.shutdownInput();
_socket.shutdownOutput();
_socket.close();
} catch (IOException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) {
e.printStackTrace();
}
}
private void openOutStreamToClient() throws IOException {
ClientRecord cl = (ClientRecord) _records.get(clientId);
Socket socketCl = cl.getClientSocket();
this.outClient = new PrintWriter(socketCl.getOutputStream(), true);
}
/*private void setClientId(){
outClient.println(CommunicationType.SET_ID.toString());
outClient.println(this.clientId);
}*/
private void sendChatroomKeyToClient() throws IOException {
outClient.println(CommunicationType.KEY.toString());
outClient.println(Base64.getEncoder().encodeToString(clientRecord.getSecretKey().getEncoded()));
}
private boolean checkMac(String clientId, String encrypted1,String mac1){
String macTest = null;
try {
macTest = EncryptionUtil.getMac(clientRecord.getSecretKey(),""+encrypted1+clientId);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
}
if (macTest.equals(mac1)){
return true;
}
else{
return false;
}
}
}