-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientOrServer.cpp
365 lines (310 loc) · 10.3 KB
/
ClientOrServer.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* This is the program for Client OR Server.
* Type "s" to choose server mode;
* Type "c" to choose client mode.
*/
#include <io.h>
#include <stdio.h>
#include <winsock2.h>
#include <string>
#include <iostream>
#include "ecn.h"
#include "big.h"
#include <ctime>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define SERVER_PORT 5208 // LISTENNING PORT
using namespace std;
char initserver(SOCKET s);
char initclient(SOCKET s);
SOCKET start();
char sendMessage();
char which();
int mutualAuthentication(char *systemType,int mode,int buffSize,int keySize,char *key,SOCKET s, char *iv);
int mutualAuthenticationRecvNonce(char *systemType,int mode,int buffSize,int keySize,char *key,SOCKET s,char *iv,char *recvNonce);
char *DH_KeyEstablishment(Big pcs, char *sessionKey);
int aesWithSessionKey(char *systemType,int mode,int buffSize,int keySize,char *key,SOCKET s,char *iv);
SOCKET start()
{
WORD wVersionRequested;
WSADATA wsaData; //This is the type for wsd data used in the winsock intiliazation
SOCKET s; //This is the type for the socket s, used to connect to a server
int ret;
cout<<"Initialising WinSock..."<<endl;
wVersionRequested=MAKEWORD(2,2); //Using WinSock Dll version
ret=WSAStartup(wVersionRequested,&wsaData);
if(ret!=0)
{
cout<<"WSAStartup() failed. Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else
cout<<"WinSock initialised."<<endl;
//Create SOCKET using TCP protocol
s=socket(AF_INET,SOCK_STREAM,0);
if(s==INVALID_SOCKET)
{
WSACleanup();
cout<<"Could not create socket. Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else
cout<<"Socket created successfully."<<endl;
return s;
}
char which()
{
char type;
// type = 'b';
cout<<"Please choose the C/S mode you want. Enter 's' for server or 'c' for client."<<endl;
type=getchar();
while((type!='s')&&(type!='c'))
{
cout<<"You typed in an inproper system type. Please try again! (Type 's' or 'c')."<<endl;
type=getchar();
}
return type;
}
int initServer(SOCKET sListen)
{
char globaltype;
int ret;
SOCKET sServer; //Used to connect with Client
struct sockaddr_in serverAddr, clientAddr; //Address information
char *serverMessage;
int addrLen;
//Prepare the sockaddr_in structure
ZeroMemory((char *)&serverAddr,sizeof(serverAddr));
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(SERVER_PORT);
serverAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
//Bind here.
ret=bind(sListen,(struct sockaddr *)&serverAddr,sizeof(serverAddr));
if(ret==SOCKET_ERROR)
{
cout<<"bind() failed! Error Code: "<<WSAGetLastError()<<endl;
closesocket(sListen); //CLOSE SOCKET
WSACleanup();
return 0;
}
else
cout<<"Bind done."<<endl;
//LISTENNING FOR CONNECTION REQUIREMENT
ret=listen(sListen,5);
if(ret==SOCKET_ERROR)
{
cout<<"listen() failed! code: "<<WSAGetLastError()<<endl;
closesocket(sListen); //CLOSE SOCKET
return 0;
}
cout<<"Waiting for client connecting!"<<endl;
cout<<"Tips: Ctrl+c to quit!"<<endl;
//Blocking to wait for client connection
while(1)
{
addrLen=sizeof(clientAddr);
sServer=accept(sListen,(struct sockaddr *)&clientAddr,&addrLen);
if(sServer==INVALID_SOCKET)
{
cout<<"accept() failed! Error Code: "<<WSAGetLastError()<<endl;
closesocket(sListen);
WSACleanup();
return 0;
}
else
cout<<"Connected successfully!"<<endl;
//Send a message back to the client
serverMessage="Congratulations! You have created a successful connection";
}
}
int initClient(SOCKET sClient)
{
int ret;
struct sockaddr_in serverAddr;
//Give server's address information
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(SERVER_PORT);
serverAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
//Connect the server
ret=connect(sClient,(struct sockaddr *)&serverAddr,sizeof(serverAddr));
if(ret==SOCKET_ERROR)
{
cout<<"connect() failed! Error Code: "<<WSAGetLastError()<<endl;
closesocket(sClient);
WSACleanup();
return 0;
}
else
cout<<"Connected the server successfully!"<<endl;
//-----------------------------------------------------------------------------------------------------------//
//Ask whether they can talk and set parameters. Choose encryption algorithms & key size & hash algorithms
while(1)
{
char *sendMessage=new char [500];
sendMessage="Here is Client. Can we talk securely? Please answer: Yes/No?";
ret=send(sClient,sendMessage,sizeof(sendMessage),0);
if(ret==SOCKET_ERROR)
{
cout<<"Request(Message1) send() failed! Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else
{
cout<<"Wait for server's reply..."<<endl;
char receiveMessage[5000];
char *ptr;
ptr=(char *)&receiveMessage;
ret=recv(sClient,ptr,5000,0);
if(ret==SOCKET_ERROR)
{
cout<<" Message1 recv() failed! Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else if(ret==0)
{
cout<<"Server has closed the connection."<<endl;
return 0;
}
while(ret>0)
{
if(strcmp((const char *)receiveMessage,"Yes")==0)
{
int mode_chosen,key_chosen;
int buffSize=16;
cout<<"Server agrees to talk. Make an agreement on what encryption algorithms you can support."<<endl;
//Client sends server what kind of algorithms he supports here
sendMessage=
"Client can only support SHA256 hash algorithms. Please choose supported encryption algorithms from below (type no.): 1.AES_ECB 2.AES_CBC 3.AES_CFB 4.AES_OFB";
ret=send(sClient,sendMessage,sizeof(sendMessage),0);
if(ret==SOCKET_ERROR)
{
cout<<"Message2 send() failed! Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else
{
cout<<"Wait for server's choice for encryption algorithm and mode..."<<endl;
//Receive server's choice for encryption algorithm and encryption mode
memset(receiveMessage,0,sizeof(receiveMessage));
ptr=(char *)&receiveMessage;
ret=recv(sClient,ptr,5000,0);
if(ret==SOCKET_ERROR)
{
cout<<"Message2 recv() failed! Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else if(ret==0)
{
cout<<"Server has closed the connection."<<endl;
return 0;
}
else if(ret>0)
{
if(strcmp((const char *)receiveMessage,"1")==0)
mode_chosen=1;
else if(strcmp((const char *)receiveMessage,"2")==0)
mode_chosen=2;
else if(strcmp((const char *)receiveMessage,"3")==0)
{mode_chosen=3;buffSize=4;}
else if(strcmp((const char *)receiveMessage,"4")==0)
mode_chosen=4;
sendMessage="Please choose a key size from below (type no.): 1.128bits 2.192bits 3.256bits";
ret=send(sClient,sendMessage,sizeof(sendMessage),0);
if(ret==SOCKET_ERROR)
{
cout<<"Message3 send() failed! Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else
{
cout<<"Wait for server's choice for key size..."<<endl;
//Receive server's choice for encryption algorithm and encryption mode
memset(receiveMessage,0,sizeof(receiveMessage));
ptr=(char *)&receiveMessage;
ret=recv(sClient,ptr,5000,0);
if(ret==SOCKET_ERROR)
{
cout<<"Message3 recv() failed! Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else if(ret==0)
{
cout<<"Server has closed the connection."<<endl;
return 0;
}
else if(ret>0)
{
if(strcmp((const char *)receiveMessage,"1")==0)
key_chosen=1;
else if(strcmp((const char *)receiveMessage,"2")==0)
key_chosen=2;
else if(strcmp((const char *)receiveMessage,"3")==0)
key_chosen=3;
}
}
}
}
cout<<"Already made an agreement on algorithms chosen."<<endl;
cout<<"Begin mutual authentication here..."<<endl;
char password[32];
cout<<"Type password: "<<endl;
//Tell server to begin read encryptionResult file, which include message for authentication
sendMessage="Begin mutual authentication.";
ret=send(sClient,sendMessage,sizeof(sendMessage),0);
if(ret==SOCKET_ERROR)
{
cout<<"Mutual authentication request send() failed! Error Code: "<<WSAGetLastError()<<endl;
return 0;
}
else
{
//-------------------------------Client read encryption file, then write() it to send it to server-----------//
mutualAuthentication(mode_chosen,buffSize,key_chosen,password);
//Define a FILE* handle to read encryption results in file EncryptionResult.bin
FILE *toReadEncryptionResult = NULL;
//Read in binary mode
fopen_s(&toReadEncryptionResult, "EncryptionResult.bin", "rb");
if (!toReadEncryptionResult) {
cerr << "Can't open encrypted file to read." << endl;
exit(EXIT_FAILURE);
}
//Compute the length of encryption results
fseek(toReadEncryptionResult, 0, SEEK_END);
int fileSize = ftell(toReadEncryptionResult);
fseek(toReadEncryptionResult, 0, SEEK_SET);
char *Block = new char[buffSize+1];
for (int i = 0; i != fileSize; i += (buffSize+1)) {
fread(Block, sizeof(char), buffSize+1, toReadEncryptionResult);
write(sClient, Block, buffSize);
}
fclose(toReadEncryptionResult);
delete []Block;
//Tell server to stop read encryptionResult file
sendMessage="End.";
send(sClient,sendMessage,sizeof(sendMessage),0);
//----------------------------------------------------------------------------------------------------------//
cout<<"Wait for server's reply..."<<endl;
memset(receiveMessage,0,sizeof(receiveMessage));
ptr=(char *)&receiveMessage;
recv(sClient,ptr,5000,0);
if(strcmp((const char *)receiveMessage,"Mutual authentication result from server.")==0)
{
FILE *AuthenticationResult=NULL;
fopen_s(&AuthenticationResult,"AuthenticationResult.bin","wb");
if (!AuthenticationResult) {
cerr << "Can't open encrypted file to write." << endl;
exit(EXIT_FAILURE);
}
}
}
}
}
char sendMessage[100];
scanf("%s",&sendMessage);
ret=send(sClient,(char *)&sendMessage,sizeof(sendMessage),0);
}
}
}
int main(int argc , char *argv[])
{
SOCKET sListen, clientServer; //Listenning socket and Link socket-attaching
char *ptr,*ptr1,*ptr2; //Pointers for message sending
}