-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp_enc.c
298 lines (265 loc) · 8.06 KB
/
otp_enc.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
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
/*********************************************************************
** Program Filename: otp_enc.c
** Author: Peter Nguyen
** Date: 3/14/16
** CS 344-400, Program 4
** Description: Sends plaintext and key to otp_enc_d and receives
** back the encrypted text.
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
const int BUFF_SIZE = 70000;
// Function prototypes
void error(const char *msg);
void writeSock(int sockfd, char* buffer);
void readSock(int sockfd, char* buffer, int size);
int validChars(char* buffer);
int main(int argc, char *argv[])
{
int sockfd,
portno,
n,
receivedNum = 0, // int representing the data size sent
returnStatus, // value returned from read or write
dataSizeNum,
convertedNum,
connectStatus;
struct sockaddr_in serv_addr;
struct hostent *server; // Defines a host computer
char txtBuffer[BUFF_SIZE],
keyBuffer[BUFF_SIZE],
ciphBuffer[BUFF_SIZE];
FILE* filePtr;
// Check for correct arguments
if (argc < 4)
{
fprintf(stderr, "usage: %s plaintext key port\n", argv[0]);
exit(1);
}
// Read the plaintext file
filePtr = fopen(argv[1], "r");
if (filePtr == NULL)
{
fprintf(stderr, "could not open plaintext file\n");
exit(1);
}
bzero(txtBuffer, BUFF_SIZE);
fgets(txtBuffer, BUFF_SIZE, filePtr);
fclose(filePtr);
// Read the key file
filePtr = fopen(argv[2], "r");
if (filePtr == NULL)
{
fprintf(stderr, "could not open key file\n");
exit(1);
}
bzero(keyBuffer, BUFF_SIZE);
fgets(keyBuffer, BUFF_SIZE, filePtr);
fclose(filePtr);
// Check for bad characters or if key file is too short
if (strlen(keyBuffer) < strlen(txtBuffer))
{
fprintf(stderr, "ERROR: key %s is too short\n", argv[2]);
exit(1);
}
if (!validChars(txtBuffer))
{
fprintf(stderr, "ERROR: bad characters in %s\n", argv[1]);
exit(1);
}
if (!validChars(keyBuffer))
{
fprintf(stderr, "ERROR: bad characters in %s\n", argv[2]);
exit(1);
}
/******** Connect to server ********/
// Set port number and host name
portno = atoi(argv[3]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("localhost");
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
// Set server address
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 the server
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on initial connect");
// Check if trying to connect to otp_dec_d; if so, reject
returnStatus = read(sockfd, &receivedNum, sizeof(receivedNum));
if (returnStatus > 0)
{
receivedNum = ntohl(receivedNum);
if (receivedNum == 2)
{
fprintf(stderr, "ERROR: could not contact otp_dec_d on port %s\n",
argv[3]);
exit(2);
}
}
// Receive the new port number from server after initial connect
returnStatus = read(sockfd, &receivedNum, sizeof(receivedNum));
if (returnStatus > 0)
receivedNum = ntohl(receivedNum);
else
fprintf(stderr, "ERROR receiving port number: %d\n", returnStatus);
// Restart socket on new port number
close(sockfd);
// Set port number and host name
portno = receivedNum; // set to new port number
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("localhost");
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(1);
}
// Set server address
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 the server
do
{
connectStatus =
connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (connectStatus < 0)
perror("ERROR on secondary connect");
} while (connectStatus < 0);
/******** Begin data exchange with server *********/
// Write the data size of plaintext to the socket
dataSizeNum = strlen(txtBuffer);
convertedNum = htonl(dataSizeNum);
returnStatus = write(sockfd, &convertedNum, sizeof(convertedNum));
if (returnStatus < 0)
error("ERROR writing data size");
// Write plaintext to the socket
writeSock(sockfd, txtBuffer);
// Write the data size of the key to the socket
dataSizeNum = strlen(keyBuffer);
convertedNum = htonl(dataSizeNum);
returnStatus = write(sockfd, &convertedNum, sizeof(convertedNum));
if (returnStatus < 0)
error("ERROR writing data size");
// Write key to the socket
writeSock(sockfd, keyBuffer);
// Read the data size of ciphertext
returnStatus = read(sockfd, &receivedNum, sizeof(receivedNum));
if (returnStatus > 0)
receivedNum = ntohl(receivedNum);
else
fprintf(stderr, "ERROR receiving data size: %d\n", returnStatus);
// Read ciphertext from the socket
readSock(sockfd, ciphBuffer, receivedNum);
printf("%s", ciphBuffer);
close(sockfd);
return 0;
}
/*********************************************************************
** writeSock
** Description: Writes data to the specified socket from the
** specified buffer in "chunks" of 1000 bytes.
** Parameters: int sockfd, char* buffer
*********************************************************************/
void writeSock(int sockfd, char* buffer)
{
int bytesWrit,
totalBytesWrit = 0,
index,
tempIndex;
char tempBuffer[BUFF_SIZE];
bytesWrit = write(sockfd, buffer, strlen(buffer));
if (bytesWrit < 0)
error("ERROR writing to socket");
else if (bytesWrit < strlen(buffer))
{
// Continue to write until all data has been sent
while (totalBytesWrit != strlen(buffer))
{
totalBytesWrit += bytesWrit;
index = totalBytesWrit + 1;
tempIndex = 0;
do
// Copy remaining data to be written into temp buffer
{
tempBuffer[tempIndex] = buffer[index];
index++;
tempIndex++;
} while (buffer[index] != '\0');
bytesWrit = write(sockfd, tempBuffer, strlen(tempBuffer));
}
}
}
/*********************************************************************
** readSock
** Description: Reads data from the specified socket to the specified
** buffer. Takes the total data size to be read as a parameter
** Parameters: int sockfd, char* buffer, int size
*********************************************************************/
void readSock(int sockfd, char* buffer, int size)
{
char tempBuffer[BUFF_SIZE];
int bytesRead;
bzero(buffer, BUFF_SIZE);
do
{
bzero(tempBuffer, BUFF_SIZE);
// Continue reading into temp buffer until buffer length = size
bytesRead = read(sockfd, tempBuffer, size);
if (bytesRead < 0)
error("ERROR reading from socket");
strcat(buffer, tempBuffer);
}
while (strlen(buffer) != size);
}
/*********************************************************************
** validChars
** Description: Checks string buffer for valid input, i.e.
** 'A' through 'Z' or space. Returns true or false.
** Parameters: char* buffer
*********************************************************************/
int validChars(char* buffer)
{
int index = 0;
while (buffer[index] != '\0')
{
if (buffer[index] < 'A' || buffer[index] > 'Z')
{
if (buffer[index] != ' ' && buffer[index] != '\n')
return 0; // false: chars are bad
}
index++;
}
return 1; // true: chars are valid
}
/*********************************************************************
** error
** Description: Displays an error message
** Parameters: const char *msg
*********************************************************************/
void error(const char *msg)
{
perror(msg);
exit(1);
}