-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
242 lines (229 loc) · 6.95 KB
/
Copy pathClient.java
File metadata and controls
242 lines (229 loc) · 6.95 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
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Description: A Chat client program that uses PKI & Secret key *
* encryption to send messages to the Chat Server *
* *
* @author Krishnan Subramanian *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* required imports for the program
*/
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ConnectException;
import java.net.Socket;
import java.net.SocketException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* Implements the Serializable interface (for object stream to be sent through
* sockets)
*
* @author Krishnan
*
*/
public class Client implements Serializable {
/**
* Required for a serializable class
*/
private static final long serialVersionUID = 1L;
private String clientPath = "";
/**
*
* @param server
* @param servPort
* @param usr
* @throws IOException
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws ClassNotFoundException
*/
public Client(String server, int servPort, String usr,String file) throws IOException,
KeyStoreException, NoSuchAlgorithmException, CertificateException,
ClassNotFoundException {
Socket socket = null;
clientPath = file;
/*
* Connect to the server & bind the socket to the server port - report
* any errors during the process
*/
try {
socket = new Socket(server, servPort);
} catch (ConnectException e) {
System.out
.println("Error occured while trying to bind socket, please check the port number for the server");
System.exit(0);
} catch (Exception e) {
System.out.println("Error occured while trying to bind socket");
System.exit(0);
}
ClientAuth ca = new ClientAuth(clientPath);
int seq = 0;
ObjectOutputStream out = null;
ObjectInputStream in = null;
System.out.println("Client initialized, connected to server "
+ socket.getInetAddress() + ":" + socket.getPort());
while (true) {
try {
++seq;
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
String input = null;
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
/**
* First sequence: Send the username encrypted using the
* server's public key [PKI-RSA]
*/
if (seq == 1) {
byte[] cusr = ca.EncryptPKI(usr.getBytes());
out.writeObject(cusr);
out.flush();
String cmsg = (String) in.readObject();
int code = Integer.parseInt(cmsg.trim());
/*
* Parse response code from the Server and notify the status
* message to the user
*/
if (code == 404) {
System.out
.println("Sorry, could not authenticate User: \""
+ usr + "\" to the server");
break;
} else if (code == 200) {
System.out.println("Trying to authenticate \"" + usr
+ "\" to the server");
continue;
} else {
System.out
.println("An error occured while trying to authenticate to the server");
break;
}
}
/**
* Second sequence: After authentication, generate a fresh DES
* key to send all future messages to the server
*/
if (seq == 2) {
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56);
SecretKey symmkey = kg.generateKey();
byte[] DESKey = symmkey.getEncoded();
byte[] enckey = ca.EncryptPKI(DESKey);
ca.SetDESKey(DESKey);
out.writeObject(enckey);
out.flush();
String cmsg = (String) in.readObject();
int code = Integer.parseInt(cmsg);
// Try to exchange the key with the Server and throw any
// errors during this process
if (code == 200) {
System.out
.println("Encrypted Key Exchange(EKE) successful with Server");
System.out
.println("At any point type \"exit\" to quit");
continue;
} else {
System.out
.println("Sorry, error occured while Encrypted Key Exchange(EKE) with Server");
break;
}
}
/**
* Read input from the user and send to the server
*/
input = br.readLine();
if (input.equals("exit")) {
break;
}
byte[] enc = ca.EncryptDES(input);
out.writeObject(enc);
out.flush();
byte[] cmsg = (byte[]) in.readObject();
String ptxt = ca.DecryptDES(cmsg);
System.out.println(ptxt);
} catch (SocketException e) {
System.out.println("Connection reset/closed by Server");
break;
} catch (NullPointerException e) {
System.out.println("Connection was closed by Server");
break;
} catch (EOFException e) {
System.out.println("Connection was closed by Client");
break;
} catch (Exception e) {
System.out.println("Oops.. an exception occured: ");
e.printStackTrace();
break;
}
}
socket.close();
System.out.println("BYE");
}
public static void main(String args[]) throws IOException,
KeyStoreException, NoSuchAlgorithmException, CertificateException,
ClassNotFoundException {
String server = "127.0.0.1";
int servPort = 1595;
String usr = "krish";
String file = "";
/**
* Check program syntax
*/
if (args.length > 4) {
System.out
.println("More than four arguments passed to the Client program");
System.out
.println("Usage: <server hostname/IP> <port> <username> <clientkeyfile path>");
System.exit(0);
} else if (args.length < 4) {
System.out
.println("Too few arguments specified for the client to start");
System.out
.println("Usage: <server hostname/IP> <port> <username> <clientkeyfile path>");
System.exit(0);
} else if (args.length == 4) {
try {
server = args[0].trim();
} catch (Exception e) {
System.out
.println("Error occured while trying to parse Server Name/IP");
System.exit(0);
}
try {
servPort = Integer.parseInt(args[1].trim());
} catch (Exception e) {
System.out
.println("Error occured while trying to parse port number");
System.exit(0);
}
try {
usr = args[2].trim();
} catch (Exception e) {
System.out
.println("Error occured while trying to get user name");
System.exit(0);
}
try {
file = args[3].trim();
} catch (Exception e) {
System.out
.println("Error occured while trying to get user name");
System.exit(0);
}
} else {
System.out
.println("Usage: <server hostname/IP> <port> <username> <clientkeyfile path>");
System.exit(0);
}
new Client(server, servPort, usr,file);
}
}