-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
227 lines (210 loc) · 10.3 KB
/
Client.java
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
// A Java program for a Client
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.security.cert.CertificateException;
import java.net.*;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
private DataInputStream inServer = null;
private String clientPasswd ="";
public SecretKey sessionKey;
private String ticket ="";
PrintWriter writer = new PrintWriter(new FileOutputStream(new File("Alice_Log.txt"),true));
// constructor to put ip address and port
public Client(String address, int port,String alice, String passwd, String serverID) throws FileNotFoundException {
// establish a connection
try
{
Commands commands = new Commands();
socket = new Socket(address, port);
//authenticate();
//System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
inServer = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//clientPasswd = inServer.readUTF();
//System.out.println(clientPasswd);
if(port == 3000) {
PublicKey pubKDC = commands.getPublicKey(System.getProperty("user.dir") + "/kdc.cert");
String ts1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now());
String step1 = commands.EncryptRSA(alice + "," + passwd + "," + serverID + "," + ts1, pubKDC);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->KDC : "+alice+" , "+passwd+" , "+serverID+" , "+ts1);
writer.flush();
out.writeUTF(step1);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->KDC : "+alice+" , "+step1);
writer.flush();
String authValue = inServer.readUTF();
while (!authValue.equals("1")) {
ts1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now());
Scanner in = new Scanner(System.in);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" KDC->"+alice+" : Password Denied");
writer.flush();
System.out.println("Your Password is incorrect! Try again");
String pass = in.nextLine();
String step = commands.EncryptRSA(alice + "," + pass + "," + serverID + "," + ts1, pubKDC);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->KDC : "+alice+" , "+pass+" , "+serverID+" , "+ts1);
writer.flush();
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->KDC : "+alice+" , "+step);
writer.flush();
out.writeUTF(step);
authValue = inServer.readUTF();
}
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" KDC->"+alice+" : Password Verified");
writer.flush();
//Step 2 başlangıcı
String step2 = inServer.readUTF();
ticket = inServer.readUTF();
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" KDC->"+alice+" : "+step2+" , "+ticket);
writer.flush();
step2 = commands.DecryptRSA(step2, System.getProperty("user.dir") + "/client.jks");
String[] parts = step2.split(",");
String strSesKey = parts[0];
this.sessionKey = commands.getSessionKey(strSesKey);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Message Decrypted : "+step2);
writer.flush();
//Step 2 sonu
try
{
//input.close();
out.close();
socket.close();
return;
}
catch(IOException i)
{
System.out.println(i);
}
}
else{
String nonce = String.valueOf(commands.generateNonce());
String t = commands.util();
t = commands.DecryptRSA(t,System.getProperty("user.dir") + "/"+serverID+".jks");
String [] parts = t.split(",");
System.out.println(parts[3]);
out.writeUTF(alice+","+commands.util()+","+commands.encrypt(nonce,commands.getSessionKey(parts[3])));
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->"+serverID+" : "+alice+" , "+nonce);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->"+serverID+" : "+alice+" , "+commands.util()+" , "+commands.encrypt(nonce,commands.getSessionKey(parts[3])));
writer.flush();
String step4 = inServer.readUTF();
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+serverID+"->"+alice+" : "+step4);
writer.flush();
step4 = commands.decrypt(step4,commands.getSessionKey(parts[3]));
String [] parts4 = step4.split(",");
String nonce4 = String.valueOf(commands.generateNonce());
if(parts4[0].equals(String.valueOf(Integer.parseInt(nonce)+1))){
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Message Decrpyted : "+parts4[0]+" is OK, N2 = "+parts4[1]);
writer.flush();
out.writeUTF(commands.encrypt(String.valueOf(Integer.parseInt(parts4[1])+1),commands.getSessionKey(parts[3])));
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->"+serverID+" : "+String.valueOf(Integer.parseInt(parts4[1])+1));
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+alice+"->"+serverID+" : "+commands.encrypt(String.valueOf(Integer.parseInt(parts4[1])+1),commands.getSessionKey(parts[3])));
writer.flush();
String auth = inServer.readUTF();
if(auth.equals("1")) {
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+serverID+"->"+alice+" : Authentication is completed!");
writer.flush();
}
else{
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" "+serverID+"->"+alice+" : Authentication is failed!");
writer.flush();
}
}
else{
writer.println("Nonce Value is not correct");
writer.flush();
}
System.exit(0);
}
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
// string to read message from input
String line = "";
// keep reading until "Over" is input
while (!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
private void authenticate(boolean isConnected,String serverId,String clientId) {
}
public static void main(String args[]) throws FileNotFoundException {
Scanner in = new Scanner(System.in);
System.out.println("Please Enter your password");
String passwd = in.nextLine();
System.out.println("Please Enter your ServerID");
String serverID = in.nextLine();
Authenticate("Alice",passwd,serverID);
//KDCStart();
}
private static void Authenticate(String alice, String passwd, String serverID) throws FileNotFoundException {
Client client = new Client("127.0.0.1", 3000, alice, passwd, serverID);
int portnum = 0;
switch (serverID) {
case "Mail":
portnum = 3001;
break;
case "Web":
portnum = 3002;
break;
case "Database":
portnum = 3003;
break;
}
Client client1 = new Client("127.0.0.1", portnum, alice, passwd, serverID);
}
}