-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
308 lines (234 loc) · 9.55 KB
/
client.cpp
File metadata and controls
308 lines (234 loc) · 9.55 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
#include "HEADER.h"
#include "ADMIN.h"
using namespace std;
bool flag=false;
string User_Name, Pass_Word;
int ENC_KEY;
string SHARED_KEY;
// User existence validation
bool User_Exists(string name){
ifstream read("updated_creds.txt");
string check, temp_email, temp_salt, temp_hash;
while (read >> check >> temp_email >> temp_salt >> temp_hash) {
if (check == name){
return true;
}
}
read.close();
return false;
}
// CHECK IF THE EMAIL IS VALID.
bool Valid_Email(string email){
return email.find('@') != string::npos && email.find('.') != string::npos;
}
//** HELPER FUNCTION TO ENSURE THAT THE KEY LENGTH IS 16-BIT */
string format_key(const string& input){
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((const unsigned char*)input.c_str(), input.size(), hash);
return string((char*)hash, 16);
}
//*SEND MESSAGES TO SERVER.*//
void send_message(int client_socket, const string& message){
send(client_socket, message.c_str(), message.size(), 0);
}
//* APPLY SIMPLE ENCRYPTION BASED UPON ROTATION. *//
string ENCRYPT(const string& message){
string result = message;
for(char& c : result){
if(isalpha(c)){
if(islower(c)){
c = 'a' + (c - 'a' + ENC_KEY) % 26;
}
else{
c = 'A' + (c - 'A' + ENC_KEY) % 26;
}
}
}
return result;
}
//*RECEIVE MESSAGES FROM SERVER AND DISPLAY THEM AS WELL.*//
bool receive_message(int client_socket, char* buf, size_t buf_size){
memset(buf, 0, buf_size);
int bytes_received = recv(client_socket, buf, buf_size - 1, 0);
if(bytes_received <= 0){
cerr<<"\n ERROR RECEIVING MESSAGES FROM THE SERVER. "<<endl;
return false;
}
buf[bytes_received] = '\0';
if(flag == false){
cout<<"\n SERVER :: "<<ENCRYPT(buf);
}
else{
string decrypted_message = AES_decrypt(buf, SHARED_KEY);
cout<<"\n [ SERVER ] :: "<<decrypted_message<<endl;
}
return true;
}
// GENERATE THE PUBLIC KEY FOR CLIENT...
long long generate_public_key(long long alpha, long long private_key, long long P){
return (long long)pow(alpha, private_key) % P;
}
// GENERATE THE SHARED KEY USING SERVER PUBLIC KEY.
long long generate_shared_key(long long received_pub_key, long long private_key, long long P){
return (long long)pow(received_pub_key, private_key) % P;
}
//*MAIN FUNCTION.*//
int main(){
HASH_CHAT();
char buf[256];
long long P, alpha, server_pub_key;
long long client_private_key = 3;
long long client_pub_key;
int client_socket;
struct sockaddr_in server_address;
bool logged_in = false;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
cerr << "\n ERROR CREATING SOCKET. " << endl;
return 0;
}
server_address.sin_family = AF_INET;
server_address.sin_port = htons(8080);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("\n CONNECTION TO SERVER FAILED. \n");
return 0;
}
// RECIEVE LARGE_P, ALPHA, & SERVER KI PUBLIC KEY...
recv(client_socket, buf, sizeof(buf), 0);
sscanf(buf, "P:%lld alpha:%lld server_pub_key:%lld", &P, &alpha, &server_pub_key);
// GENERATE PUBLIC KEY FOR CLIENT.
client_pub_key = generate_public_key(alpha, client_private_key, P);
snprintf(buf, sizeof(buf), "%lld", client_pub_key);
send(client_socket, buf, sizeof(buf), 0);
// CALCUALTE THE SHARED KEY.
long long shared_key_client = generate_shared_key(server_pub_key, client_private_key, P);
ENC_KEY = shared_key_client;
while(true){
if(!logged_in){
if(!receive_message(client_socket, buf, sizeof(buf))) {
break;
}
string choice;
cout << "\n HIT THE KEYS = ";
getline(cin, choice);
send_message(client_socket, choice + "\n");
if (choice == "1") {
string username, email, password, confirm_pass;
do {
sleep(1);
receive_message(client_socket, buf, sizeof(buf));
getline(cin, username);
while(User_Exists(username)){
cout<<"\n USERNAME ALREADY EXISTS. TRY ANOTHER ONE: ";
getline(cin, username);
}
username = ENCRYPT(username);
send_message(client_socket, username + "\n");
} while (string( ENCRYPT (buf) ).find("USER NAME ALREADY EXISTS") != string::npos);
do {
sleep(1);
receive_message(client_socket, buf, sizeof(buf));
getline(cin, email);
while(!Valid_Email(email)){
cout<<"\n PLEASE ENTER A VALID EMAIL FORMAT (e.g. 123@example.com): ";
getline(cin, email);
}
email = ENCRYPT(email);
sleep(1);
send_message(client_socket, email + "\n");
memset(buf, 0, sizeof(buf));
}while(string( ENCRYPT (buf) ).find("INVALID EMAIL FORMAT") != string::npos);
do {
receive_message(client_socket, buf, sizeof(buf));
getline(cin, password);
password = ENCRYPT(password);
sleep(1);
send_message(client_socket, password + "\n");
memset(buf, 0, sizeof(buf));
}while(string( ENCRYPT (buf) ).find("WEAK PASSWORD") != string::npos);
receive_message(client_socket, buf, sizeof(buf));
memset(buf, 0, sizeof(buf));
}
else if (choice == "2"){
sleep(1);
receive_message(client_socket, buf, sizeof(buf));
getline(cin, User_Name);
User_Name = ENCRYPT(User_Name);
send_message(client_socket, User_Name + "\n");
sleep(1);
receive_message(client_socket, buf, sizeof(buf));
getline(cin, Pass_Word);
Pass_Word = ENCRYPT(Pass_Word);
send_message(client_socket, Pass_Word + "\n");
if (receive_message(client_socket, buf, sizeof(buf))) {
if (string( ENCRYPT (buf) ).find("LOGIN SUCCESSFUL") != string::npos) {
logged_in = true;
cout << "\nYOU CAN NOW CHAT WITH THE SERVER.\n";
}
}
}
else if (choice=="3"){
string username, password;
sleep(1);
receive_message(client_socket, buf, sizeof(buf));
getline(cin, username);
username = ENCRYPT(username);
send_message(client_socket, username + "\n");
sleep(1);
receive_message(client_socket, buf, sizeof(buf));
getline(cin, password);
password = ENCRYPT(password);
send_message(client_socket, password + "\n");
if(receive_message(client_socket, buf, sizeof(buf))){
if(string( ENCRYPT (buf) ).find("LOGIN SUCCESSFUL") != string::npos){
cout<<ANSI_COLOR_PURPLE<< "\n YOU ARE NOW GRANTED ELEVATED PRIVILLAGES."<<ANSI_COLOR_RESET<<endl;
memset(buf, 0, sizeof(buf));
ADMIN admin;
admin.Admin_Menu();
}
}
continue;
}
else if(choice=="0"){
cout<<ANSI_COLOR_BOLD_RED<<"\n EXITING THE CHAT. BYE."<<ANSI_COLOR_RESET<<endl;
close(client_socket);
return 0;
}
}
if(logged_in){
flag = true;
// APPEND THE SHARED KEY WITH THE USERNAME [WHO LOGED IN]
User_Name = ENCRYPT(User_Name);
string aes_key = User_Name + to_string(APPEND_KEY);
// MAKE THE KEY LENGTH [16-BIT]
SHARED_KEY = format_key(aes_key);
while(true){
string message;
cout<<ANSI_COLOR_BOLD_GREEN<< "\n [ YOU ] :: ";
getline(cin, message);
if(message == "exit"){
send_message(client_socket, "exit\n");
cout<<ANSI_COLOR_RESET<<endl;
return 0;
}
// ENCRYPT MESSAGE BEFORE SENDING.
string encrypted_message = AES_encrypt(message, SHARED_KEY);
send(client_socket, encrypted_message.c_str(), encrypted_message.size(), 0);
char buf[1024];
memset(buf, 0, sizeof(buf));
int bytes_received = recv(client_socket, buf, sizeof(buf), 0);
if (bytes_received <= 0) {
cout<<ANSI_COLOR_BOLD_RED<<"\n CONNECTION CLOSED BY THE SERVER. "<<ANSI_COLOR_RESET<<endl;
break;
}
// DECRYPT MESSAGE BEFORE DISPLAYING.
string encrypted_response(buf, bytes_received);
string decrypted_response = AES_decrypt(encrypted_response, SHARED_KEY);
cout<<"\n[SERVER] => "<<decrypted_response;
}
}
}// while
close(client_socket);
return 0;
}//function