-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp_enc.c
182 lines (151 loc) · 6.4 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
/*************************************************************************************
* Author: Mae LaPresta
* Title: otp_enc
* Description: Sends a message to encode and a key to encode it with to a server, and
* receives the encoded message in return.
*************************************************************************************/
#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 <fcntl.h>
void error(const char *msg) { perror(msg); exit(1); }
int verifyString(char *inputString, int length);
int main(int argc, char *argv[])
{
int socketFD, portNumber, charsWritten, charsRead;
struct sockaddr_in serverAddress;
struct hostent* serverHostInfo;
char buffer[1000];
char *totalMsg;
char *totalKey;
char encodedMsg[70000];
uint32_t msgLength;
uint32_t msgLengthbeforeConvert;
uint32_t keyLength;
int file_descriptor;
ssize_t nread;
char confirm[3];
int charsRecCount;
char identityMsg[]="enc";
if (argc < 4) { fprintf(stderr,"USAGE: %s plaintext key port\n", argv[0]); exit(0); }
//Open the file containing the message to encode
file_descriptor = open(argv[1], O_RDONLY);
if (file_descriptor<0){
fprintf(stderr, "Could not open %s\n", argv[1]);
exit(1);
}
lseek(file_descriptor, 0, SEEK_SET);
msgLengthbeforeConvert=lseek(file_descriptor, 0, SEEK_END);
totalMsg=malloc(msgLengthbeforeConvert*sizeof(char));
memset(totalMsg, '\0', msgLengthbeforeConvert);
lseek(file_descriptor, 0, SEEK_SET);
nread = read(file_descriptor, totalMsg, msgLengthbeforeConvert);
close(file_descriptor);
totalMsg[msgLengthbeforeConvert-1]='\0';
//Open the file containing the key to encode
file_descriptor = open(argv[2], O_RDONLY);
if (file_descriptor<0){
fprintf(stderr, "Could not open %s\n", argv[2]);
exit(1);
}
lseek(file_descriptor, 0, SEEK_SET);
keyLength=lseek(file_descriptor, 0, SEEK_END);
if (keyLength<msgLengthbeforeConvert) {
fprintf(stderr, "Error: key '%s' is too short\n", argv[2]);
exit(1);
}
lseek(file_descriptor, 0, SEEK_SET);
totalKey=malloc(msgLengthbeforeConvert*sizeof(char));
memset(totalKey, '\0', msgLengthbeforeConvert);
nread = read(file_descriptor, totalKey, msgLengthbeforeConvert);
close(file_descriptor);
totalKey[msgLengthbeforeConvert-1]='\0';
//verify that all of the characters in the message string are either an uppercase alphabet character or a space
if (!verifyString(totalMsg, msgLengthbeforeConvert)){
fprintf(stderr, "otp_enc error: input contains bad characters\n" );
exit(1);
};
//verify that all of the characters in the key string are either an uppercase alphabet character or a space
if (!verifyString(totalKey, msgLengthbeforeConvert)){
fprintf(stderr, "otp_enc error: input contains bad characters\n" );
exit(1);
}
// Set up the server address struct
memset((char*)&serverAddress, '\0', sizeof(serverAddress));
portNumber = atoi(argv[3]);
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(portNumber);
serverHostInfo = gethostbyname("localhost");
if (serverHostInfo == NULL) { fprintf(stderr, "Error: could not contact otp_enc_d on port %d", portNumber); exit(2); }
memcpy((char*)&serverAddress.sin_addr.s_addr, (char*)serverHostInfo->h_addr, serverHostInfo->h_length);
// Set up the socket
socketFD = socket(AF_INET, SOCK_STREAM, 0);
if (socketFD < 0) error("CLIENT: ERROR opening socket");
// Connect to server
if (connect(socketFD, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0){
fprintf(stderr, "Error: could not contact otp_enc_d on port %d", portNumber);
exit(2);
}
// Send message of client ID to server
charsWritten = send(socketFD, identityMsg, strlen(identityMsg), 0);
if (charsWritten < 0) error("CLIENT: ERROR writing to socket\n");
//get the server response to whether the connection is OK or not
memset(confirm, '\0', sizeof(confirm));
memset(buffer, '\0', sizeof(buffer));
charsRead = recv(socketFD, buffer, sizeof(buffer)-1, 0);
if (charsRead < 0) error("CLIENT: ERROR reading from socket\n");
strcpy(confirm, buffer);
if (strcmp(confirm, "OK")){
fprintf(stderr, "Error: could not contact otp_enc_d on port %d\n", portNumber);
exit(2);
}
//send the message length to the server
msgLength=htonl(msgLengthbeforeConvert);
charsWritten = send(socketFD, &msgLength, sizeof(uint32_t), 0);
if (charsWritten < 0) error("CLIENT: ERROR writing to socket\n");
//get the server confirmation that it is ok to send the message
memset(buffer, '\0', sizeof(confirm));
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0);
if (charsRead < 0) error("CLIENT: ERROR reading from socket\n");
//send the message to be encoded to the server
charsWritten = send(socketFD, totalMsg, strlen(totalMsg), 0);
if (charsWritten < 0) error("CLIENT: ERROR writing to socket\n");
//get confimation that the server is ready to receive the key
memset(buffer, '\0', sizeof(buffer));
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0);
if (charsRead < 0) error("CLIENT: ERROR reading from socket\n");
//send the key to the server
charsWritten = send(socketFD, totalKey, strlen(totalKey), 0);
if (charsWritten < 0) error("CLIENT: ERROR writing to socket\n");
//get the encoded message from the server
charsRecCount=0;
memset(encodedMsg, '\0', strlen(encodedMsg));
while (charsRecCount<msgLengthbeforeConvert-1){
memset(buffer, '\0', 1000);
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0);
if (charsRead < 0) error("ERROR reading from socket\n");
charsRecCount+=charsRead;
strcat(encodedMsg, buffer);
}
printf("%s\n", encodedMsg);
close(socketFD);
free(totalMsg);
free(totalKey);
return 0;
}
int verifyString(char *inputString, int length){
int i;
int valid=1;
for (i=0; i<length-1; i++){
if (!((inputString[i]>='A'&&inputString[i]<='Z')||inputString[i]==' ')){
valid=0;
break;
}
}
return valid;
}