-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient_77_3.java
154 lines (122 loc) · 5.91 KB
/
Client_77_3.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
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Scanner;
public class Client_77_3 {
private static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
/* Arguments:
args[0] = ID
args[1] = Server IP "localhost"
*/
int clientID = Integer.parseInt(args[0]);
String serverIP = args[1];
int serverPort = 40000;
Socket s = null;
DataInputStream in = null;
DataOutputStream out = null;
try {
s = new Socket(serverIP, serverPort);
// Creating the data streams
in = new DataInputStream(s.getInputStream());
out = new DataOutputStream(s.getOutputStream());
// Generating the secret key & cipher
SecretKeySpec secretKey = SecurityUtil_77_3.generateAESKey();
Cipher cipher = Cipher.getInstance("AES");
// Prompting user for credentials
System.out.println("Enter your username:");
String username = scanner.nextLine();
System.out.println("Enter your password:");
String password = scanner.nextLine();
// Sending encrypted username and password to the server
out.writeUTF(SecurityUtil_77_3.encrypt(username, cipher, secretKey));
out.writeUTF(SecurityUtil_77_3.encrypt(password, cipher, secretKey));
// Receiving OTP from the server
String encryptedOtp = in.readUTF();
String otp = SecurityUtil_77_3.decrypt(encryptedOtp, cipher, secretKey);
// if received isn't otp, either username || password are incorrect
if (otp.equals("authentication_failed")) {
System.out.println("Authentication failed. Exiting...");
return;
}
System.out.println("Received OTP: " + otp);
// Prompting user for OTP input
System.out.println("Enter the OTP received:");
String userOTP = scanner.nextLine();
// Sending encrypted OTP input to the server
out.writeUTF(SecurityUtil_77_3.encrypt(userOTP, cipher, secretKey));
// Receive authentication response from the server
String encyptedResponse = in.readUTF();
String authenticationResponse = SecurityUtil_77_3.decrypt(encyptedResponse, cipher, secretKey);
if (authenticationResponse.equalsIgnoreCase("authenticated")) {
System.out.println("Welcome to the Event Management System!");
// If authenticated, receive event data from the serve
String upcomingEvents = in.readUTF();
System.out.println(SecurityUtil_77_3.decrypt(upcomingEvents, cipher, secretKey));
String ActiveEvents = in.readUTF();
System.out.println(SecurityUtil_77_3.decrypt(ActiveEvents, cipher, secretKey));
// Prompt user for action
System.out.println("\nPlease enter your action (join [event_id]):");
String action = scanner.nextLine();
// Adding client's ID to the message
String fullMessage = action + " " + clientID;
// Sending requested event & client ID
String encryptedMessage = SecurityUtil_77_3.encrypt(fullMessage, cipher, secretKey);
out.writeUTF(encryptedMessage);
while (true) {
// Read input from the server
if (in.available() > 0) {
// Receiving & Decrypting message from server
String encryptedData = in.readUTF();
String decryptedData = SecurityUtil_77_3.decrypt(encryptedData, cipher, secretKey);
System.out.println(decryptedData);
//Checking is server is expecting response (message doesn't start with ---)
if (!decryptedData.startsWith("---") && !decryptedData.startsWith("\n---")) {
String output = scanner.nextLine();
// Encrypting & sending user response
encryptedMessage = SecurityUtil_77_3.encrypt(output, cipher, secretKey);
out.writeUTF(encryptedMessage);
}
}
}
} else {
System.out.println("Authentication failed. Exiting...");
}
} catch (UnknownHostException e) {
System.out.println("Error Socket:" + e.getMessage());
} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
System.out.println("Error closing socket: " + e.getMessage());
}
}
}
}
}