Holy shit please test
This commit is contained in:
parent
fdeb0b4cb8
commit
3d4a6f521f
11 changed files with 760 additions and 4 deletions
151
pads/dec_client.c
Normal file
151
pads/dec_client.c
Normal file
|
@ -0,0 +1,151 @@
|
||||||
|
#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 <errno.h>
|
||||||
|
|
||||||
|
void error(const char *msg) { perror(msg); exit(0); }
|
||||||
|
void sendFile(int, char*);
|
||||||
|
void checkLen(char*, char*);
|
||||||
|
char* toString(char*);
|
||||||
|
void checkChars(char*, char*);
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
int socketFD, portNumber, charsWritten, charsRead;
|
||||||
|
struct sockaddr_in serverAddress;
|
||||||
|
struct hostent* serverHostInfo;
|
||||||
|
char* buffer = malloc(sizeof(char) * 70001);
|
||||||
|
|
||||||
|
if (argc < 4) { perror("Failed to launch correctly"); exit(1); }
|
||||||
|
|
||||||
|
checkLen(argv[1], argv[2]);
|
||||||
|
char* plain = toString(argv[1]);
|
||||||
|
char* key = toString(argv[2]);
|
||||||
|
checkChars(plain, "plain");
|
||||||
|
checkChars(key, "key");
|
||||||
|
|
||||||
|
// 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, "CLIENT: ERROR, no such host\n"); exit(0); }
|
||||||
|
|
||||||
|
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) // Connect socket to addy
|
||||||
|
fprintf(stderr, "Cannot connect to Server: %d\n", atoi(argv[1]));
|
||||||
|
|
||||||
|
// Check Correct Server
|
||||||
|
send(socketFD, "dec_client", 10, 0);
|
||||||
|
charsWritten = recv(socketFD, buffer, 12, 0);
|
||||||
|
if(!strncmp(buffer, "Wrong Client", 12)) {
|
||||||
|
fprintf(stderr, "Server Has Rejected the Client: %d\n", atoi(argv[3]));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* final = malloc(sizeof(char) * 140002);
|
||||||
|
memcpy(final, plain, 70001);
|
||||||
|
memcpy(final+70001, key, 70001);
|
||||||
|
|
||||||
|
// Send plaintext to server
|
||||||
|
charsWritten = send(socketFD, final, 140002, 0);
|
||||||
|
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
|
||||||
|
if (charsWritten < strlen(plain)) printf("CLIENT: WARNING: Not all data written to socket!\n");
|
||||||
|
//printf("strlen1: %ld\n", strlen(plain) );
|
||||||
|
//printf("chars1: %d\n", charsWritten);
|
||||||
|
|
||||||
|
//charsWritten = recv(socketFD, buffer, 3, 0);
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Send key to server
|
||||||
|
charsWritten = send(socketFD, key, 70001, 0);
|
||||||
|
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
|
||||||
|
if (charsWritten < strlen(key)) printf("CLIENT: WARNING: Not all data written to socket!\n");
|
||||||
|
printf("strlen2: %ld\n", strlen(key) );
|
||||||
|
printf("chars2: %d\n", charsWritten);
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Get Cyphertext from server
|
||||||
|
memset(buffer, '\0', 70001);
|
||||||
|
charsRead = recv(socketFD, buffer, 70001, 0);
|
||||||
|
|
||||||
|
if (charsRead < 0) error("CLIENT: ERROR reading from socket");
|
||||||
|
//printf("strlen3: %ld\n", strlen(buffer) );
|
||||||
|
//printf("chars3: %d\n", charsRead);
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
close(socketFD);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* toString(char* file) {
|
||||||
|
|
||||||
|
char* buffer;
|
||||||
|
size_t length;
|
||||||
|
FILE * f = fopen (file, "rb");
|
||||||
|
|
||||||
|
if (f)
|
||||||
|
{
|
||||||
|
fseek (f, 0, SEEK_END);
|
||||||
|
length = ftell (f);
|
||||||
|
fseek (f, 0, SEEK_SET);
|
||||||
|
//buffer = malloc (length + 1);
|
||||||
|
buffer = malloc (70001);
|
||||||
|
memset(buffer, '\0', 70001);
|
||||||
|
if (buffer)
|
||||||
|
{
|
||||||
|
fread (buffer, 1, length, f);
|
||||||
|
buffer[length] = '\0';
|
||||||
|
}
|
||||||
|
fclose (f);
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkLen(char* plain, char* key) {
|
||||||
|
|
||||||
|
FILE *p = fopen(plain, "r");
|
||||||
|
if(errno !=0) {
|
||||||
|
perror(plain);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
FILE *k = fopen(key, "r");
|
||||||
|
if(errno != 0) {
|
||||||
|
perror(key);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(p, 0, SEEK_END);
|
||||||
|
fseek(k, 0, SEEK_END);
|
||||||
|
|
||||||
|
if(ftell(k) < ftell(p)) {
|
||||||
|
fprintf(stderr, "Keyfile Too Small: exiting...\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void checkChars(char* str, char* type) {
|
||||||
|
|
||||||
|
for(int i = 0; i < strlen(str)-1; i++) {
|
||||||
|
if(str[i] == ' ');
|
||||||
|
else if(str[i] > 64 && str[i] < 91);
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "Invalid Character in %s file: Exiting...\n", type);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
158
pads/dec_server.c
Normal file
158
pads/dec_server.c
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
void error(const char *msg) { perror(msg); exit(1); }
|
||||||
|
void onClient(int);
|
||||||
|
void clean_children();
|
||||||
|
int to_c(int);
|
||||||
|
int from_c(int);
|
||||||
|
int neg_mod(int, int);
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
int listenSocketFD, establishedConnectionFD, portNumber;
|
||||||
|
struct sockaddr_in serverAddress;
|
||||||
|
|
||||||
|
if (argc < 2) { fprintf(stderr,"USAGE: %s port\n", argv[0]); exit(1); }
|
||||||
|
|
||||||
|
// Set up the address struct for this process (the server)
|
||||||
|
memset((char *)&serverAddress, '\0', sizeof(serverAddress)); // Clear out the address struct
|
||||||
|
portNumber = atoi(argv[1]);
|
||||||
|
serverAddress.sin_family = AF_INET;
|
||||||
|
serverAddress.sin_port = htons(portNumber);
|
||||||
|
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
// Set up the socket
|
||||||
|
listenSocketFD = socket(AF_INET, SOCK_STREAM, 0); // Create the socket
|
||||||
|
if (listenSocketFD < 0) error("ERROR opening socket");
|
||||||
|
|
||||||
|
// Enable the socket to begin listening
|
||||||
|
if (bind(listenSocketFD, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0)
|
||||||
|
error("ERROR on binding");
|
||||||
|
|
||||||
|
listen(listenSocketFD, 5);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
clean_children();
|
||||||
|
onClient(listenSocketFD);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(listenSocketFD);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onClient(int listenSocketFD) {
|
||||||
|
|
||||||
|
// Accept a connection, blocking if one is not available until one connects
|
||||||
|
struct sockaddr_in clientAddress;
|
||||||
|
socklen_t sizeOfClientInfo = sizeof(clientAddress);
|
||||||
|
int establishedConnectionFD = accept(listenSocketFD, (struct sockaddr *)&clientAddress, &sizeOfClientInfo);
|
||||||
|
|
||||||
|
if (establishedConnectionFD < 0) error("ERROR on accept");
|
||||||
|
|
||||||
|
int f = fork();
|
||||||
|
|
||||||
|
if(!f) {
|
||||||
|
|
||||||
|
size_t charsRead;
|
||||||
|
char* buffer1 = malloc(sizeof(char) * 70001);
|
||||||
|
char* buffer2 = malloc(sizeof(char) * 70001);
|
||||||
|
|
||||||
|
charsRead = recv(establishedConnectionFD, buffer1, 10, 0);
|
||||||
|
// check for proper client
|
||||||
|
if(!strncmp(buffer1, "dec_client", 10)) {
|
||||||
|
send(establishedConnectionFD, "Goood Client", 12, 0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
send(establishedConnectionFD, "Wrong Client", 12, 0);
|
||||||
|
close(establishedConnectionFD);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* final = malloc(sizeof(char) * 140002);
|
||||||
|
|
||||||
|
memset(final, '\0', 140002);
|
||||||
|
charsRead = recv(establishedConnectionFD, final, 140002, MSG_WAITALL);
|
||||||
|
if (charsRead < 0) error("ERROR reading from socket");
|
||||||
|
printf("char1: %ld\n", charsRead);
|
||||||
|
|
||||||
|
memcpy(buffer1, final, 70001);
|
||||||
|
memcpy(buffer2, final + 70001, 70001);
|
||||||
|
|
||||||
|
//charsRead = send(establishedConnectionFD, "ack", 3, 0);
|
||||||
|
|
||||||
|
/*
|
||||||
|
memset(buffer2, '\0', 70001);
|
||||||
|
charsRead = recv(establishedConnectionFD, buffer2, 70001, 0);
|
||||||
|
if (charsRead < 0) error("ERROR reading from socket");
|
||||||
|
printf("char2: %ld\n", charsRead);
|
||||||
|
*/
|
||||||
|
|
||||||
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer1);
|
||||||
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer2);
|
||||||
|
|
||||||
|
printf("strlen: %ld\n", strlen(buffer1));
|
||||||
|
// do enc
|
||||||
|
char* cyphertext = malloc(sizeof(char) * 70001);
|
||||||
|
memset(cyphertext, '\0', strlen(buffer1)+1);
|
||||||
|
for(int i = 0; i < strlen(buffer1)-1; i++) {
|
||||||
|
cyphertext[i] = from_c(neg_mod((to_c(buffer1[i]) - to_c(buffer2[i])), 27));
|
||||||
|
}
|
||||||
|
//printf("Cyphertext: %s\n", cyphertext);
|
||||||
|
|
||||||
|
// Send a Success message back to the client
|
||||||
|
charsRead = send(establishedConnectionFD, cyphertext, 70001, 0);
|
||||||
|
|
||||||
|
if (charsRead < 0) error("ERROR writing to socket");
|
||||||
|
close(establishedConnectionFD);
|
||||||
|
free(buffer1);
|
||||||
|
free(buffer2);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clean_children() {
|
||||||
|
|
||||||
|
// Clean up any child process that has not been cleaned
|
||||||
|
int pid = 43110;
|
||||||
|
while(pid >0) {
|
||||||
|
int status;
|
||||||
|
pid = waitpid(0, &status, WNOHANG | WUNTRACED );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int to_c(int c) {
|
||||||
|
int ret;
|
||||||
|
if(c == ' ')
|
||||||
|
ret = 26;
|
||||||
|
else
|
||||||
|
ret = c - 'A';
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int from_c(int c) {
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if(c == 26)
|
||||||
|
ret = 32;
|
||||||
|
else
|
||||||
|
ret = c + 'A';
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int neg_mod(int num, int op) {
|
||||||
|
|
||||||
|
if(num > -1)
|
||||||
|
return num % op;
|
||||||
|
else
|
||||||
|
return num + op;
|
||||||
|
|
||||||
|
}
|
141
pads/enc_client.c
Normal file
141
pads/enc_client.c
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
#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 <errno.h>
|
||||||
|
|
||||||
|
void error(const char *msg) { perror(msg); exit(0); }
|
||||||
|
void sendFile(int, char*);
|
||||||
|
void checkLen(char*, char*);
|
||||||
|
char* toString(char*);
|
||||||
|
void checkChars(char*, char*);
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
int socketFD, portNumber, charsWritten, charsRead;
|
||||||
|
struct sockaddr_in serverAddress;
|
||||||
|
struct hostent* serverHostInfo;
|
||||||
|
char buffer[70000];
|
||||||
|
|
||||||
|
if (argc < 4) { fprintf(stderr,"USAGE: %s hostname port\n", argv[0]); exit(0); }
|
||||||
|
|
||||||
|
checkLen(argv[1], argv[2]);
|
||||||
|
char* plain = toString(argv[1]);
|
||||||
|
char* key = toString(argv[2]);
|
||||||
|
checkChars(plain, "plain");
|
||||||
|
checkChars(key, "key");
|
||||||
|
|
||||||
|
// 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, "CLIENT: ERROR, no such host\n"); exit(0); }
|
||||||
|
|
||||||
|
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) // Connect socket to addy
|
||||||
|
fprintf(stderr, "Cannot connect to Server: %d\n", atoi(argv[1]));
|
||||||
|
|
||||||
|
// Check Correct Server
|
||||||
|
send(socketFD, "enc_client", 10, 0);
|
||||||
|
charsWritten = recv(socketFD, buffer, 12, 0);
|
||||||
|
if(!strncmp(buffer, "Wrong Client", 12)) {
|
||||||
|
fprintf(stderr, "Server Has Rejected the Client: %d\n", atoi(argv[3]));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send plaintext to server
|
||||||
|
charsWritten = send(socketFD, plain, 70001, 0);
|
||||||
|
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
|
||||||
|
if (charsWritten < strlen(plain)) printf("CLIENT: WARNING: Not all data written to socket!\n");
|
||||||
|
//printf("%d\n",charsWritten);
|
||||||
|
|
||||||
|
// Send key to server
|
||||||
|
charsWritten = send(socketFD, key, 70001, 0);
|
||||||
|
if (charsWritten < 0) error("CLIENT: ERROR writing to socket");
|
||||||
|
if (charsWritten < strlen(key)) printf("CLIENT: WARNING: Not all data written to socket!\n");
|
||||||
|
//printf("%d\n",charsWritten);
|
||||||
|
|
||||||
|
// Get Cyphertext from server
|
||||||
|
memset(buffer, '\0', sizeof(buffer));
|
||||||
|
charsRead = recv(socketFD, buffer, sizeof(buffer) - 1, 0);
|
||||||
|
//printf("%d\n", charsRead);
|
||||||
|
|
||||||
|
if (charsRead < 0) error("CLIENT: ERROR reading from socket");
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
close(socketFD);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* toString(char* file) {
|
||||||
|
|
||||||
|
int charsWritten = 0;
|
||||||
|
char* buffer;
|
||||||
|
size_t length;
|
||||||
|
FILE * f = fopen (file, "rb");
|
||||||
|
|
||||||
|
if (f)
|
||||||
|
{
|
||||||
|
fseek (f, 0, SEEK_END);
|
||||||
|
length = ftell (f);
|
||||||
|
fseek (f, 0, SEEK_SET);
|
||||||
|
//buffer = malloc (length);
|
||||||
|
buffer = malloc (sizeof(char) * 70001);
|
||||||
|
memset(buffer, '\0', 70001);
|
||||||
|
if (buffer)
|
||||||
|
{
|
||||||
|
fread (buffer, 1, length, f);
|
||||||
|
buffer[length-1] = '\0';
|
||||||
|
}
|
||||||
|
fclose (f);
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkLen(char* plain, char* key) {
|
||||||
|
|
||||||
|
FILE *p = fopen(plain, "r");
|
||||||
|
if(errno != 0) {
|
||||||
|
perror(plain);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
FILE *k = fopen(key, "r");
|
||||||
|
if(errno != 0 ) {
|
||||||
|
perror(key);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(p, 0, SEEK_END);
|
||||||
|
fseek(k, 0, SEEK_END);
|
||||||
|
|
||||||
|
if(ftell(k) < ftell(p)) {
|
||||||
|
fprintf(stderr, "Keyfile Too Small: exiting...\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void checkChars(char* str, char* type) {
|
||||||
|
|
||||||
|
for(int i = 0; i < strlen(str); i++) {
|
||||||
|
if(str[i] == ' ');
|
||||||
|
else if(str[i] > 64 && str[i] < 91);
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "Invalid Character in %s file: Exiting...\n", type);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
137
pads/enc_server.c
Normal file
137
pads/enc_server.c
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
void error(const char *msg) { perror(msg); exit(1); }
|
||||||
|
void onClient(int);
|
||||||
|
void clean_children();
|
||||||
|
int to_c(int);
|
||||||
|
int from_c(int);
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
int listenSocketFD, establishedConnectionFD, portNumber;
|
||||||
|
struct sockaddr_in serverAddress;
|
||||||
|
|
||||||
|
if (argc < 2) { fprintf(stderr,"USAGE: %s port\n", argv[0]); exit(1); }
|
||||||
|
|
||||||
|
// Set up the address struct for this process (the server)
|
||||||
|
memset((char *)&serverAddress, '\0', sizeof(serverAddress)); // Clear out the address struct
|
||||||
|
portNumber = atoi(argv[1]);
|
||||||
|
serverAddress.sin_family = AF_INET;
|
||||||
|
serverAddress.sin_port = htons(portNumber);
|
||||||
|
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
// Set up the socket
|
||||||
|
listenSocketFD = socket(AF_INET, SOCK_STREAM, 0); // Create the socket
|
||||||
|
if (listenSocketFD < 0) error("ERROR opening socket");
|
||||||
|
|
||||||
|
// Enable the socket to begin listening
|
||||||
|
if (bind(listenSocketFD, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0)
|
||||||
|
error("ERROR on binding");
|
||||||
|
|
||||||
|
listen(listenSocketFD, 5);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
clean_children();
|
||||||
|
onClient(listenSocketFD);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(listenSocketFD);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onClient(int listenSocketFD) {
|
||||||
|
|
||||||
|
// Accept a connection, blocking if one is not available until one connects
|
||||||
|
struct sockaddr_in clientAddress;
|
||||||
|
socklen_t sizeOfClientInfo = sizeof(clientAddress);
|
||||||
|
int establishedConnectionFD = accept(listenSocketFD, (struct sockaddr *)&clientAddress, &sizeOfClientInfo);
|
||||||
|
|
||||||
|
if (establishedConnectionFD < 0) error("ERROR on accept");
|
||||||
|
|
||||||
|
int f = fork();
|
||||||
|
|
||||||
|
if(!f) {
|
||||||
|
|
||||||
|
int charsRead;
|
||||||
|
char* buffer1 = malloc(sizeof(char) * 70001);
|
||||||
|
char* buffer2 = malloc(sizeof(char) * 70001);
|
||||||
|
|
||||||
|
charsRead = recv(establishedConnectionFD, buffer1, 10, 0);
|
||||||
|
// check for proper client
|
||||||
|
if(!strncmp(buffer1, "enc_client", 10)) {
|
||||||
|
send(establishedConnectionFD, "Goood Client", 12, 0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
send(establishedConnectionFD, "Wrong Client", 12, 0);
|
||||||
|
close(establishedConnectionFD);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(buffer1, '\0', 70001);
|
||||||
|
charsRead = recv(establishedConnectionFD, buffer1, 70001, MSG_WAITALL);
|
||||||
|
if (charsRead < 0) error("ERROR reading from socket");
|
||||||
|
printf("%d\n", charsRead);
|
||||||
|
|
||||||
|
memset(buffer2, '\0', 70001);
|
||||||
|
charsRead = recv(establishedConnectionFD, buffer2, 70001, MSG_WAITALL);
|
||||||
|
if (charsRead < 0) error("ERROR reading from socket");
|
||||||
|
printf("%d\n", charsRead);
|
||||||
|
|
||||||
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer1);
|
||||||
|
//printf("SERVER: I received this from the client: \"%s\"\n", buffer2);
|
||||||
|
printf("strlen: %ld\n", strlen(buffer1));
|
||||||
|
|
||||||
|
// do enc
|
||||||
|
char* cyphertext = malloc(70001);
|
||||||
|
memset(cyphertext, '\0', strlen(buffer1)+1);
|
||||||
|
for(int i = 0; i < strlen(buffer1); i++) {
|
||||||
|
cyphertext[i] = from_c((to_c(buffer1[i]) + to_c(buffer2[i])) % 27);
|
||||||
|
}
|
||||||
|
//printf("Cyphertext: %s\n", cyphertext);
|
||||||
|
|
||||||
|
// Send a Success message back to the client
|
||||||
|
charsRead = send(establishedConnectionFD, cyphertext, 70001, 0);
|
||||||
|
|
||||||
|
if (charsRead < 0) error("ERROR writing to socket");
|
||||||
|
close(establishedConnectionFD);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clean_children() {
|
||||||
|
|
||||||
|
// Clean up any child process that has not been cleaned
|
||||||
|
int pid = 43110;
|
||||||
|
while(pid >0) {
|
||||||
|
int status;
|
||||||
|
pid = waitpid(0, &status, WNOHANG | WUNTRACED );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int to_c(int c) {
|
||||||
|
int ret;
|
||||||
|
if(c == ' ')
|
||||||
|
ret = 26;
|
||||||
|
else
|
||||||
|
ret = c - 'A';
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int from_c(int c) {
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if(c == 26)
|
||||||
|
ret = 32;
|
||||||
|
else
|
||||||
|
ret = c + 'A';
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
|
@ -1,13 +1,28 @@
|
||||||
CC=gcc --std=gnu99 -g
|
CC=gcc --std=gnu99 -g
|
||||||
|
|
||||||
all: keygen
|
all: keygen enc_client enc_server dec_client dec_server
|
||||||
echo Everything Made
|
echo Everything Made
|
||||||
|
|
||||||
keygen: keygen.c
|
keygen: keygen.c
|
||||||
$(CC) keygen.c -o keygen
|
$(CC) keygen.c -o keygen
|
||||||
|
|
||||||
run: all
|
enc_client: enc_client.c
|
||||||
./$(output)
|
$(CC) enc_client.c -o enc_client
|
||||||
|
|
||||||
|
enc_server: enc_server.c
|
||||||
|
$(CC) enc_server.c -o enc_server
|
||||||
|
|
||||||
|
dec_client: dec_client.c
|
||||||
|
$(CC) dec_client.c -o dec_client
|
||||||
|
|
||||||
|
dec_server: dec_server.c
|
||||||
|
$(CC) dec_server.c -o dec_server
|
||||||
|
|
||||||
|
ec: all
|
||||||
|
./enc_client plaintext1 key1 11111
|
||||||
|
|
||||||
|
dc: all
|
||||||
|
./dec_client cyphertext1 key1 22222
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -fr *.o vgcore.* $(output)
|
rm -fr *.o vgcore.* keygen enc_client enc_server dec_client dec_server
|
||||||
|
|
149
pads/p5testscript
Executable file
149
pads/p5testscript
Executable file
|
@ -0,0 +1,149 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# FYI, this command removes file abc if it is empty: [ -s abc ] || rm -f abc
|
||||||
|
|
||||||
|
usage="usage: $0 encryptionport decryptionport"
|
||||||
|
|
||||||
|
#use the standard version of echo
|
||||||
|
echo=/bin/echo
|
||||||
|
|
||||||
|
#Make sure we have the right number of arguments
|
||||||
|
if test $# -gt 2 -o $# -lt 2
|
||||||
|
then
|
||||||
|
${echo} $usage 1>&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
#Clean up any previous runs
|
||||||
|
${echo} '#Initializing - Cleaning up - ignore Operation Not Permitted errors'
|
||||||
|
${echo} '#Note: this script requires that the current directory (.) be in your PATH in ~/.bashrc'
|
||||||
|
killall -q -u $USER dec_client
|
||||||
|
killall -q -u $USER dec_server
|
||||||
|
killall -q -u $USER enc_client
|
||||||
|
killall -q -u $USER enc_server
|
||||||
|
rm -f ciphertext*
|
||||||
|
rm -f plaintext*_*
|
||||||
|
rm -f key20
|
||||||
|
rm -f key70000
|
||||||
|
|
||||||
|
#Record the ports passed in
|
||||||
|
encport=$1
|
||||||
|
decport=$2
|
||||||
|
|
||||||
|
#Run the daemons
|
||||||
|
./enc_server $encport &
|
||||||
|
./dec_server $decport &
|
||||||
|
|
||||||
|
sleep 5
|
||||||
|
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#START OF GRADING SCRIPT'
|
||||||
|
${echo} '#keygen 20 > key20'
|
||||||
|
./keygen 20 > key20
|
||||||
|
${echo} "#5 POINTS: key20 must exist"
|
||||||
|
[ -s key20 ] || rm -f key20
|
||||||
|
if [ -f key20 ]; then ${echo} 'key20 exists!'; else ${echo} 'key20 DOES NOT EXIST'; fi
|
||||||
|
${echo}
|
||||||
|
${echo} "#-----------------------------------------"
|
||||||
|
${echo} "#5 POINTS: Number of characters in key20, should be 21:"
|
||||||
|
wc -m key20
|
||||||
|
${echo}
|
||||||
|
${echo} "#-----------------------------------------"
|
||||||
|
${echo} '#keygen 70000 > key70000'
|
||||||
|
./keygen 70000 > key70000
|
||||||
|
${echo} "#5 POINTS: Number of characters in key70000, should be 70001:"
|
||||||
|
[ -s key70000 ] || rm -f key70000
|
||||||
|
wc -m key70000
|
||||||
|
${echo}
|
||||||
|
${echo} "#-----------------------------------------"
|
||||||
|
${echo} '#enc_client plaintext1 key20 $encport'
|
||||||
|
${echo} "#10 POINTS: Should return error about too-short key"
|
||||||
|
./enc_client plaintext1 key20 $encport
|
||||||
|
${echo}
|
||||||
|
${echo} "#-----------------------------------------"
|
||||||
|
${echo} '#enc_client plaintext1 key70000 $encport'
|
||||||
|
${echo} "#20 POINTS: Should return encrypted version of plaintext1"
|
||||||
|
./enc_client plaintext1 key70000 $encport
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#enc_client plaintext1 key70000 $encport > ciphertext1'
|
||||||
|
./enc_client plaintext1 key70000 $encport > ciphertext1
|
||||||
|
${echo} "#10 POINTS: ciphertext1 must exist"
|
||||||
|
[ -s ciphertext1 ] || rm -f ciphertext1
|
||||||
|
if [ -f ciphertext1 ]; then ${echo} 'ciphertext1 exists!'; else ${echo} 'ciphertext1 DOES NOT EXIST'; fi
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#10 POINTS: ciphertext1 must be same number of chars as source'
|
||||||
|
${echo} '#wc -m plaintext1'
|
||||||
|
wc -m plaintext1
|
||||||
|
${echo} '#Should be same: wc -m ciphertext1'
|
||||||
|
wc -m ciphertext1
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#5 POINTS: ciphertext1 should look encrypted'
|
||||||
|
cat ciphertext1
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#dec_client ciphertext1 key70000 $encport'
|
||||||
|
${echo} '#5 POINTS: Should fail giving error that dec_client cannot use enc_server'
|
||||||
|
./dec_client ciphertext1 key70000 $encport
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#20 POINTS: should return decrypted ciphertext1 that matches source'
|
||||||
|
${echo} '#cat plaintext1'
|
||||||
|
cat plaintext1
|
||||||
|
${echo} '#dec_client ciphertext1 key70000 $decport'
|
||||||
|
./dec_client ciphertext1 key70000 $decport
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#dec_client ciphertext1 key70000 $decport > plaintext1_a'
|
||||||
|
./dec_client ciphertext1 key70000 $decport > plaintext1_a
|
||||||
|
${echo} "#10 POINTS: plaintext1_a must exist"
|
||||||
|
[ -s plaintext1_a ] || rm -f plaintext1_a
|
||||||
|
if [ -f plaintext1_a ]; then ${echo} 'plaintext1_a exists!'; else ${echo} 'plaintext1_a DOES NOT EXIST'; fi
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#cmp plaintext1 plaintext1_a'
|
||||||
|
${echo} '#5 POINTS: plaintext1 must be the same as plaintext1_a:'
|
||||||
|
${echo} '#echo $? should be == 0, which means the cmp succeeded!'
|
||||||
|
cmp plaintext1 plaintext1_a
|
||||||
|
echo $?
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#20 POINTS: concurrent test of encryption - look for 4 properly-sized ciphertext# files, or 5 where the 5th is 0 bytes'
|
||||||
|
${echo} '#5 POINTS: Should be only one error about plaintext5 being bad'
|
||||||
|
rm -f ciphertext*
|
||||||
|
rm -f plaintext*_*
|
||||||
|
./enc_client plaintext1 key70000 $encport > ciphertext1 &
|
||||||
|
./enc_client plaintext2 key70000 $encport > ciphertext2 &
|
||||||
|
./enc_client plaintext3 key70000 $encport > ciphertext3 &
|
||||||
|
./enc_client plaintext4 key70000 $encport > ciphertext4 &
|
||||||
|
./enc_client plaintext5 key70000 $encport > ciphertext5 &
|
||||||
|
${echo} 'Ten second sleep, your program must complete in this time'
|
||||||
|
sleep 10
|
||||||
|
ls -pla
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#15 POINTS: concurrent test of decryption - look for 4 plaintext#_a files that match the plaintext# files'
|
||||||
|
./dec_client ciphertext1 key70000 $decport > plaintext1_a &
|
||||||
|
./dec_client ciphertext2 key70000 $decport > plaintext2_a &
|
||||||
|
./dec_client ciphertext3 key70000 $decport > plaintext3_a &
|
||||||
|
./dec_client ciphertext4 key70000 $decport > plaintext4_a &
|
||||||
|
${echo} '#Ten second sleep, your program must complete in this time'
|
||||||
|
sleep 10
|
||||||
|
ls -pla
|
||||||
|
|
||||||
|
#Clean up
|
||||||
|
${echo}
|
||||||
|
${echo} '#-----------------------------------------'
|
||||||
|
${echo} '#Cleaning up - ignore Operation Not Permitted errors'
|
||||||
|
killall -q -u $USER dec_client
|
||||||
|
killall -q -u $USER dec_server
|
||||||
|
killall -q -u $USER enc_client
|
||||||
|
killall -q -u $USER enc_server
|
||||||
|
rm -f ciphertext*
|
||||||
|
rm -f plaintext*_*
|
||||||
|
rm -f key20
|
||||||
|
rm -f key70000
|
||||||
|
${echo}
|
||||||
|
${echo} '#SCRIPT COMPLETE'
|
1
pads/plaintext1
Normal file
1
pads/plaintext1
Normal file
|
@ -0,0 +1 @@
|
||||||
|
THE RED GOOSE FLIES AT MIDNIGHT STOP
|
1
pads/plaintext2
Normal file
1
pads/plaintext2
Normal file
|
@ -0,0 +1 @@
|
||||||
|
CAVERNA POWER GRID SPACE CADETS STARSHIP TROOPERS DIXIT DOMINION ARKHAM HORROR MAGIC FALLING BANE ECLIPSE ACQUIRE CORE WORLDS REVOLUTION LORDS OF WATERDEEP MICE AND MYSTICS PATHFINDER ACG MUNCHKIN SMASHUP TWILIGHT STRUGGLE PUERTO RICO SMALL WORLD PANDEMIC SEVEN WONDERS ROBORALLY DIPLOMACY DEADWOOD KILL DOCTOR LUCKY
|
1
pads/plaintext3
Normal file
1
pads/plaintext3
Normal file
|
@ -0,0 +1 @@
|
||||||
|
IN THE BEGINNING
|
1
pads/plaintext4
Normal file
1
pads/plaintext4
Normal file
File diff suppressed because one or more lines are too long
1
pads/plaintext5
Normal file
1
pads/plaintext5
Normal file
|
@ -0,0 +1 @@
|
||||||
|
$*!(#*djs8301these-are-all-bad-characters
|
Loading…
Reference in a new issue