-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
201 lines (156 loc) · 4.67 KB
/
client.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#define modulo 128
void error(const char *msg)
{
perror(msg);
printf("closing connection with the server\n");
exit(0);
}
// generate private key
uint8_t generate_private_key()
{
srand(time(0));
uint8_t private_key = rand()%modulo;
return private_key;
}
// generate public key
uint8_t generate_public_key(uint8_t private_key)
{
uint8_t public_key = modulo - private_key;
return public_key;
}
// Encrypt function for messages
char* encrypt(uint8_t public_key,char* message)
{
//printf("original message=%s\n",message);
int n = strlen(message);
char* temp = (char *)malloc(n+1);
strcpy(temp, message);
for (int i=0;i<n;i++){
char c = (temp[i] + public_key) % modulo;
temp[i]=c;
}
//printf("Encrypted message=%s\n",temp);
return (char *)temp;
}
// Decrypt funtion for messsages
void decrypt(uint8_t private_key,char* message)
{
//printf("message to de deccrypted=%s\n",message);
int n = strlen(message);
for (int i=0;i<n;i++){
char c = (message[i] + private_key) % modulo;
message[i]=c;
}
//printf("message after decryption=%s\n",message);
}
// main funtion
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[100],client_name[20],server_name[20];
char end[]="end connection";
if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
// create stream socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
//build server address structure
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
//connect to server
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
else{printf("\nConnected to server successfully.Say hi to server to start\n");}
// enter your user name
printf("Enter your username : ");
fgets(client_name,20,stdin);
char* first_newline = strchr(client_name, '\n');
if (first_newline)
*first_newline = '\0';
// generate private key
uint8_t private_key= generate_private_key();
// generate public key
uint8_t public_key = generate_public_key(private_key);
// send public key to server
n = write(sockfd,&public_key,sizeof(uint8_t));
if (n < 0)
error("ERROR writing to socket");
// get public key from server
uint8_t public_key_server;
n = read(sockfd,&public_key_server,sizeof(uint8_t));
if (n < 0)
error("ERROR writing to socket");
// send username to server
char *encrypted_message;
encrypted_message = encrypt(public_key_server,client_name);
n = write(sockfd,encrypted_message,strlen(encrypted_message));
if (n < 0)
error("ERROR writing to socket");
// read server name
n = read(sockfd, server_name, 20);
decrypt(private_key,server_name);
if (n < 0)
error("ERROR reading from socket");
printf("\nStart chat...\n");
// send and receive data from server
while(1)
{
// clean buffer
bzero(buffer,100);
// print on screen and type messsage
printf("%s : ",client_name);
fgets(buffer,100,stdin);
char* first_newline = strchr(buffer, '\n');
if (first_newline)
*first_newline = '\0';
// send message to server
char *encrypted_message;
encrypted_message = encrypt(public_key_server,buffer);
n=write(sockfd,encrypted_message,strlen(encrypted_message));
if (n < 0)
error("ERROR writing to socket");
// check for condition for exit
int i = strncmp("Bye",buffer,3);
if(i==0)
break;
// read message from server and decrpyt
bzero(buffer,100);
n=read(sockfd,buffer,100);
if (n < 0)
error("ERROR reading from socket");
decrypt(private_key,buffer);
// print on screen
printf("%s : %s\n",server_name, buffer);
}
printf("\nClosing connection from Server\n");
close(sockfd);
return 0;
}