From 7207ab32cdb629d77cbf1fcc7786e4519cd1ba60 Mon Sep 17 00:00:00 2001 From: Seppia Date: Wed, 15 Jun 2016 16:12:22 +0200 Subject: moved sources to appropriate directories --- keygen.c | 60 ------------- old_code/keygen.c | 60 +++++++++++++ old_code/read_crypt.c | 43 +++++++++ old_code/rosicchio.c | 43 +++++++++ onetimebluh.c | 244 -------------------------------------------------- read_crypt.c | 43 --------- rosicchio.c | 43 --------- src/onetimebluh.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 390 insertions(+), 390 deletions(-) delete mode 100644 keygen.c create mode 100644 old_code/keygen.c create mode 100644 old_code/read_crypt.c create mode 100644 old_code/rosicchio.c delete mode 100644 onetimebluh.c delete mode 100644 read_crypt.c delete mode 100644 rosicchio.c create mode 100644 src/onetimebluh.c diff --git a/keygen.c b/keygen.c deleted file mode 100644 index b03b89c..0000000 --- a/keygen.c +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -#include - -int main(int argc, char* argv[]) { - - int opt; - int nbytes = 256; - char* defname = "default.key"; - char* output; - int defout = 0; - int defbyte = 0; - int noopts = 0; - while ((opt = getopt(argc, argv, "b:o:")) != -1) { - switch (opt) { - case 'b': - nbytes = atoi(optarg); - defbyte = 1; - break; - case 'o': - output = argv[(optind-1)]; - defout = 1; - break; - default: - fprintf(stderr, "Usage: %s [-b nbytes] [-o output] \n", argv[0]); - exit(EXIT_FAILURE); - } - if (noopts == 0) { - noopts = 1; - } - } - - if(noopts == 0) { - printf("WARNING no option specified usign default values... \n"); - } - if (nbytes < 0) { - printf("Negative byte value inserted! \n"); - printf("Exiting... \n"); - exit(EXIT_FAILURE); - } else if (nbytes != 0) { - if (defbyte == 0) { - printf("No byte number specified... using default value: 256 \n"); - } - unsigned char key[nbytes]; - RAND_bytes(key, nbytes); - if (defout == 0) { - output = defname; - printf("No output name specified... using default value: default.key \n"); - } - FILE* file = fopen(output, "w"); - fwrite(key, nbytes, 1, file); - fclose(file); - printf("Created key file %s of %d bytes \n", output, nbytes); - } else { - printf("Byte number specified is 0. \n"); - printf("Doing nothing! \n"); - } - - exit(EXIT_SUCCESS); -} diff --git a/old_code/keygen.c b/old_code/keygen.c new file mode 100644 index 0000000..b03b89c --- /dev/null +++ b/old_code/keygen.c @@ -0,0 +1,60 @@ +#include +#include +#include + +int main(int argc, char* argv[]) { + + int opt; + int nbytes = 256; + char* defname = "default.key"; + char* output; + int defout = 0; + int defbyte = 0; + int noopts = 0; + while ((opt = getopt(argc, argv, "b:o:")) != -1) { + switch (opt) { + case 'b': + nbytes = atoi(optarg); + defbyte = 1; + break; + case 'o': + output = argv[(optind-1)]; + defout = 1; + break; + default: + fprintf(stderr, "Usage: %s [-b nbytes] [-o output] \n", argv[0]); + exit(EXIT_FAILURE); + } + if (noopts == 0) { + noopts = 1; + } + } + + if(noopts == 0) { + printf("WARNING no option specified usign default values... \n"); + } + if (nbytes < 0) { + printf("Negative byte value inserted! \n"); + printf("Exiting... \n"); + exit(EXIT_FAILURE); + } else if (nbytes != 0) { + if (defbyte == 0) { + printf("No byte number specified... using default value: 256 \n"); + } + unsigned char key[nbytes]; + RAND_bytes(key, nbytes); + if (defout == 0) { + output = defname; + printf("No output name specified... using default value: default.key \n"); + } + FILE* file = fopen(output, "w"); + fwrite(key, nbytes, 1, file); + fclose(file); + printf("Created key file %s of %d bytes \n", output, nbytes); + } else { + printf("Byte number specified is 0. \n"); + printf("Doing nothing! \n"); + } + + exit(EXIT_SUCCESS); +} diff --git a/old_code/read_crypt.c b/old_code/read_crypt.c new file mode 100644 index 0000000..af3a7c0 --- /dev/null +++ b/old_code/read_crypt.c @@ -0,0 +1,43 @@ +#include +#include +#include + +int main(int argc, char *argv[]) { +//reads a message <4096B and XORs it with key argv[1] in file argv[2] +//message ends with string "@@@" after a white character. +//To retrieve the original message, use onetimebluh.c +FILE * keyfile = fopen(argv[1],"r"); +FILE * crypt = fopen(argv[2],"w"); +char msg[4096]; + +printf("Write a message.\n It will be encrypted in file %s ",argv[2]); +printf("with key %s\n",argv[1]); +printf("When you're done, use the EOF sequence @@@. It must follow a white space!\n"); + +char c_aux; +int i=0,k=0; +//scanf terminates at every white character, must be called by a While +// "i" tells where you are in msg (which is overwritten each time) +// "k" tells where you are in crypt +while(strcmp(msg,"@@@")) + { + scanf("%s",msg); + i=0; + while(msg[i]!='\0') { + fscanf(keyfile,"%c", &c_aux); + fprintf(crypt,"%c",msg[i]^c_aux); + i++; + k++; + } + fscanf(keyfile,"%c",&c_aux); + fprintf(crypt,"%c",' '^c_aux); + k++; + } + +while (k<4096){ +// fills the message with @s + fscanf(keyfile,"%c",&c_aux); + fprintf(crypt,"%c",'@'^c_aux); + k++;} +fclose(crypt); +} diff --git a/old_code/rosicchio.c b/old_code/rosicchio.c new file mode 100644 index 0000000..ffd3ee8 --- /dev/null +++ b/old_code/rosicchio.c @@ -0,0 +1,43 @@ +#include +#include +#include + +int main(int argc, char* argv[]) { + + FILE* mess = fopen(argv[1], "r"); + FILE* pad = fopen(argv[2], "r+"); + FILE* critt = fopen(argv[3],"w"); + fseek(mess, 0L, SEEK_END); + long mess_size = ftell(mess); + rewind(mess); + fseek(pad, 0L, SEEK_END); + long pad_size = ftell(pad); + fseek(pad, (pad_size - mess_size), SEEK_SET); + int i = 1; + char a, b; + + while(i != EOF) { + i = fscanf(mess, "%c", &a); + fscanf(pad, "%c", &b); + if(i != EOF) { + fprintf(critt, "%c", a^b); + } + } + + /* The truncate function "rosicchia" la parte di chiave utilizzata */ + + ftruncate(fileno(pad), (pad_size - mess_size)); + fseek(pad, 0L, SEEK_END); + long new_pad_size = ftell(pad); + + fclose(mess); + fclose(pad); + fclose(critt); + + printf("Message length is %li bytes \n", mess_size); + printf("Pad length was %li bytes \n", pad_size); + printf("Pad length is now %li bytes \n", new_pad_size); + printf("Pad length should now be %li bytes \n", (pad_size - mess_size)); + + exit(EXIT_SUCCESS); +} diff --git a/onetimebluh.c b/onetimebluh.c deleted file mode 100644 index 8c46524..0000000 --- a/onetimebluh.c +++ /dev/null @@ -1,244 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -void xor(int ed, char* mess, char* keyf, char* outp); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ -void keyrand(int nb, char* outp); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ -void help(char* av[]); /* prints the help message */ - -int main(int argc, char* argv[]) { - - int opt = 1; - int command = 0; - int comm = 0; - int tear = 0; - char* message = NULL; - char* keyfile = NULL; - char* output = NULL; - int nbytes = -1; // must be resolved temporary workaround (ho sonno) - - /* The following while cycle parses the argv vector to find commands, options and relative arguments - using the function getopt_long */ - - while (opt) { - int option_index = 0; - static struct option options[] = { - {"decrypt", required_argument, 0, 'd'}, - {"encrypt", required_argument, 0, 'e'}, - {"key-file", required_argument, 0, 'k'}, - {"key-gen", no_argument, 0, 'g'}, - {"help", no_argument, 0, 'h'}, - {"nbytes", required_argument, 0, 'b'}, - {"output", required_argument, 0, 'o'}, - {"tear-page", no_argument, 0, 't'}, - {0, 0, 0, 0}, - }; - - if ((opt = getopt_long(argc, argv, "b:d:e:ghk:o:t", options, &option_index)) == -1) - break; - - switch (opt) { - case 'b': - nbytes = atoi(argv[optind-1]); - break; - case 'd': - message = argv[optind-1]; - command++; - comm = 'u'; - break; - case 'e': - message = argv[optind-1]; - if (access(message, F_OK) == -1) { /* checks the existence of the file and eventually exits */ - error(errno, errno, message); - } - command++; - comm = 'e'; - break; - case 'g': - command++; - comm = 'g'; - break; - case 'h': - help(argv); - command++; - break; - case 'k': - keyfile = argv[optind-1]; - if (access(keyfile, F_OK) == -1) { /* look at the comment before */ - error(errno, errno, keyfile); - } - break; - case 'o': - output = argv[optind-1]; - break; - case 't': - tear = 1; - break; - case '?': - break; - default: - printf("carachter code returned 0%o \n", opt); - } - } - - /* Next section performs some input checks */ - - if (command == 0) { - printf("No command called \n"); - exit(EXIT_FAILURE); - } else if (command > 1) { - printf("Multiple commands called \n"); - printf("%s [COMMAND] [OPTIONS] ... \n",argv[0]); - exit(EXIT_FAILURE); - } - - if (optind < argc) { - printf("Too many arguments \n"); - exit(EXIT_FAILURE); - } - - /* Next section detects the functions to call */ - - if (comm == 'e' || comm == 'u') { - if (keyfile == NULL) { - printf("No key specified: exit! \n"); - exit(EXIT_FAILURE); - } - if (tear == 1) { - comm++; - } - xor(comm, message, keyfile, output); - } else if (comm == 'g') { - keyrand(nbytes, output); - } - - exit(EXIT_SUCCESS); -} - -void xor(int ed, char* mess, char* keyf, char* outp) { - - char* defenoutp = "critt"; - char* defdeoutp = "decritt"; - - /* In absence of input by users nex block sets the default values */ - - if (outp == NULL) { - if (ed == 'e' || ed == 'f') { - printf("WARNING no output name specified using default value 'critt' \n"); - outp = defenoutp; - } else if (ed == 'u' || ed == 'v') { - printf("WARNING no output name specified usign default value 'decritt' \n"); - outp = defdeoutp; - } - } - - FILE* mex = fopen(mess, "r"); - FILE* keyx = fopen(keyf, "r+"); - FILE* critt = fopen(outp, "w"); - - long mess_size; - long pad_size; - - if (ed == 'f' || ed == 'v') { - fseek(mex, 0L, SEEK_END); - mess_size = ftell(mex); - rewind(mex); - fseek(keyx, 0L, SEEK_END); - pad_size = ftell(keyx); - fseek(keyx, (pad_size - mess_size), SEEK_SET); - } - - int i = 1; - char a, b; - - while (i != EOF) { - i = fscanf(mex, "%c", &a); - fscanf(keyx, "%c", &b); - if(i != EOF) { - fprintf(critt, "%c", a^b); - } - } - - if (ed == 'f' || ed == 'v') { - ftruncate(fileno(keyx), (pad_size - mess_size)); - fseek(keyx, 0L, SEEK_END); - long new_pad_size = ftell(keyx); - printf("Your pad is now %li bytes shorter \n", mess_size); - printf("You now have %li bytes left \n", new_pad_size); - } - - fclose(mex); - fclose(keyx); - fclose(critt); - - if (ed == 'e' || ed == 'f') { - printf("Message successfully encrypted \n"); - } else if (ed == 'u' || ed == 'v') { - printf("Message successfully decrypted \n"); - } - - return; -} - -void keyrand(int nb, char* outp) { - - char* defoutp = "default.key"; - - /* Next block controls the inputs and eventually sets the default values */ - - if((nb == -1) && (outp == NULL)) { - printf("WARNING no option specified usign default values... \n"); - nb = 256; - outp = defoutp; - } - if (nb < -1) { // orribile - printf("Negative byte value inserted! \n"); - printf("Exiting... \n"); - exit(EXIT_FAILURE); - } else if (nb != 0) { - if(nb == -1) { - printf("No byte number specified... using default value: 256 \n"); - nb = 256; - } - - unsigned char key[nb]; - RAND_bytes(key, nb); - - if (outp == NULL) { - outp = defoutp; - printf("No output name specified... using default value: default.key \n"); - } - - FILE* file = fopen(outp, "w"); - fwrite(key, sizeof(char), nb, file); - fclose(file); - printf("Created key file %s of %d bytes \n", outp, nb); - } else { - printf("Byte number specified is 0. \n"); - printf("Doing nothing! \n"); - } - - return; -} - -void help(char* av[]) { - fprintf(stdout,"ONETIMEBLUH USAGE: \n" - "%s [COMMAND] [OPTIONS] \n \n" - "COMMANDS: \n" - "-d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling \n" - "-e, --encrypt=FILE encrypt message (input) \n" - "-h, --help print this help \n" - "-g, --key-gen create key file \n \n" - "OPTIONS \n" - "-k, --key-file=KEY_FILE use key (input) \n" - "-b, --nbytes=NUM number of bytes \n" - "-o, --output=FILE output name \n" - "-t, --tear-page deletes from the pad file the bytes used to encrypt/decrypt \n \n" - "Onetimebluh project repository at https://git.eigenlab.org/Seppia/onetimebluh \n", av[0]); - - return; -} diff --git a/read_crypt.c b/read_crypt.c deleted file mode 100644 index af3a7c0..0000000 --- a/read_crypt.c +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include - -int main(int argc, char *argv[]) { -//reads a message <4096B and XORs it with key argv[1] in file argv[2] -//message ends with string "@@@" after a white character. -//To retrieve the original message, use onetimebluh.c -FILE * keyfile = fopen(argv[1],"r"); -FILE * crypt = fopen(argv[2],"w"); -char msg[4096]; - -printf("Write a message.\n It will be encrypted in file %s ",argv[2]); -printf("with key %s\n",argv[1]); -printf("When you're done, use the EOF sequence @@@. It must follow a white space!\n"); - -char c_aux; -int i=0,k=0; -//scanf terminates at every white character, must be called by a While -// "i" tells where you are in msg (which is overwritten each time) -// "k" tells where you are in crypt -while(strcmp(msg,"@@@")) - { - scanf("%s",msg); - i=0; - while(msg[i]!='\0') { - fscanf(keyfile,"%c", &c_aux); - fprintf(crypt,"%c",msg[i]^c_aux); - i++; - k++; - } - fscanf(keyfile,"%c",&c_aux); - fprintf(crypt,"%c",' '^c_aux); - k++; - } - -while (k<4096){ -// fills the message with @s - fscanf(keyfile,"%c",&c_aux); - fprintf(crypt,"%c",'@'^c_aux); - k++;} -fclose(crypt); -} diff --git a/rosicchio.c b/rosicchio.c deleted file mode 100644 index ffd3ee8..0000000 --- a/rosicchio.c +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include - -int main(int argc, char* argv[]) { - - FILE* mess = fopen(argv[1], "r"); - FILE* pad = fopen(argv[2], "r+"); - FILE* critt = fopen(argv[3],"w"); - fseek(mess, 0L, SEEK_END); - long mess_size = ftell(mess); - rewind(mess); - fseek(pad, 0L, SEEK_END); - long pad_size = ftell(pad); - fseek(pad, (pad_size - mess_size), SEEK_SET); - int i = 1; - char a, b; - - while(i != EOF) { - i = fscanf(mess, "%c", &a); - fscanf(pad, "%c", &b); - if(i != EOF) { - fprintf(critt, "%c", a^b); - } - } - - /* The truncate function "rosicchia" la parte di chiave utilizzata */ - - ftruncate(fileno(pad), (pad_size - mess_size)); - fseek(pad, 0L, SEEK_END); - long new_pad_size = ftell(pad); - - fclose(mess); - fclose(pad); - fclose(critt); - - printf("Message length is %li bytes \n", mess_size); - printf("Pad length was %li bytes \n", pad_size); - printf("Pad length is now %li bytes \n", new_pad_size); - printf("Pad length should now be %li bytes \n", (pad_size - mess_size)); - - exit(EXIT_SUCCESS); -} diff --git a/src/onetimebluh.c b/src/onetimebluh.c new file mode 100644 index 0000000..8c46524 --- /dev/null +++ b/src/onetimebluh.c @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +#include +#include + +void xor(int ed, char* mess, char* keyf, char* outp); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ +void keyrand(int nb, char* outp); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ +void help(char* av[]); /* prints the help message */ + +int main(int argc, char* argv[]) { + + int opt = 1; + int command = 0; + int comm = 0; + int tear = 0; + char* message = NULL; + char* keyfile = NULL; + char* output = NULL; + int nbytes = -1; // must be resolved temporary workaround (ho sonno) + + /* The following while cycle parses the argv vector to find commands, options and relative arguments + using the function getopt_long */ + + while (opt) { + int option_index = 0; + static struct option options[] = { + {"decrypt", required_argument, 0, 'd'}, + {"encrypt", required_argument, 0, 'e'}, + {"key-file", required_argument, 0, 'k'}, + {"key-gen", no_argument, 0, 'g'}, + {"help", no_argument, 0, 'h'}, + {"nbytes", required_argument, 0, 'b'}, + {"output", required_argument, 0, 'o'}, + {"tear-page", no_argument, 0, 't'}, + {0, 0, 0, 0}, + }; + + if ((opt = getopt_long(argc, argv, "b:d:e:ghk:o:t", options, &option_index)) == -1) + break; + + switch (opt) { + case 'b': + nbytes = atoi(argv[optind-1]); + break; + case 'd': + message = argv[optind-1]; + command++; + comm = 'u'; + break; + case 'e': + message = argv[optind-1]; + if (access(message, F_OK) == -1) { /* checks the existence of the file and eventually exits */ + error(errno, errno, message); + } + command++; + comm = 'e'; + break; + case 'g': + command++; + comm = 'g'; + break; + case 'h': + help(argv); + command++; + break; + case 'k': + keyfile = argv[optind-1]; + if (access(keyfile, F_OK) == -1) { /* look at the comment before */ + error(errno, errno, keyfile); + } + break; + case 'o': + output = argv[optind-1]; + break; + case 't': + tear = 1; + break; + case '?': + break; + default: + printf("carachter code returned 0%o \n", opt); + } + } + + /* Next section performs some input checks */ + + if (command == 0) { + printf("No command called \n"); + exit(EXIT_FAILURE); + } else if (command > 1) { + printf("Multiple commands called \n"); + printf("%s [COMMAND] [OPTIONS] ... \n",argv[0]); + exit(EXIT_FAILURE); + } + + if (optind < argc) { + printf("Too many arguments \n"); + exit(EXIT_FAILURE); + } + + /* Next section detects the functions to call */ + + if (comm == 'e' || comm == 'u') { + if (keyfile == NULL) { + printf("No key specified: exit! \n"); + exit(EXIT_FAILURE); + } + if (tear == 1) { + comm++; + } + xor(comm, message, keyfile, output); + } else if (comm == 'g') { + keyrand(nbytes, output); + } + + exit(EXIT_SUCCESS); +} + +void xor(int ed, char* mess, char* keyf, char* outp) { + + char* defenoutp = "critt"; + char* defdeoutp = "decritt"; + + /* In absence of input by users nex block sets the default values */ + + if (outp == NULL) { + if (ed == 'e' || ed == 'f') { + printf("WARNING no output name specified using default value 'critt' \n"); + outp = defenoutp; + } else if (ed == 'u' || ed == 'v') { + printf("WARNING no output name specified usign default value 'decritt' \n"); + outp = defdeoutp; + } + } + + FILE* mex = fopen(mess, "r"); + FILE* keyx = fopen(keyf, "r+"); + FILE* critt = fopen(outp, "w"); + + long mess_size; + long pad_size; + + if (ed == 'f' || ed == 'v') { + fseek(mex, 0L, SEEK_END); + mess_size = ftell(mex); + rewind(mex); + fseek(keyx, 0L, SEEK_END); + pad_size = ftell(keyx); + fseek(keyx, (pad_size - mess_size), SEEK_SET); + } + + int i = 1; + char a, b; + + while (i != EOF) { + i = fscanf(mex, "%c", &a); + fscanf(keyx, "%c", &b); + if(i != EOF) { + fprintf(critt, "%c", a^b); + } + } + + if (ed == 'f' || ed == 'v') { + ftruncate(fileno(keyx), (pad_size - mess_size)); + fseek(keyx, 0L, SEEK_END); + long new_pad_size = ftell(keyx); + printf("Your pad is now %li bytes shorter \n", mess_size); + printf("You now have %li bytes left \n", new_pad_size); + } + + fclose(mex); + fclose(keyx); + fclose(critt); + + if (ed == 'e' || ed == 'f') { + printf("Message successfully encrypted \n"); + } else if (ed == 'u' || ed == 'v') { + printf("Message successfully decrypted \n"); + } + + return; +} + +void keyrand(int nb, char* outp) { + + char* defoutp = "default.key"; + + /* Next block controls the inputs and eventually sets the default values */ + + if((nb == -1) && (outp == NULL)) { + printf("WARNING no option specified usign default values... \n"); + nb = 256; + outp = defoutp; + } + if (nb < -1) { // orribile + printf("Negative byte value inserted! \n"); + printf("Exiting... \n"); + exit(EXIT_FAILURE); + } else if (nb != 0) { + if(nb == -1) { + printf("No byte number specified... using default value: 256 \n"); + nb = 256; + } + + unsigned char key[nb]; + RAND_bytes(key, nb); + + if (outp == NULL) { + outp = defoutp; + printf("No output name specified... using default value: default.key \n"); + } + + FILE* file = fopen(outp, "w"); + fwrite(key, sizeof(char), nb, file); + fclose(file); + printf("Created key file %s of %d bytes \n", outp, nb); + } else { + printf("Byte number specified is 0. \n"); + printf("Doing nothing! \n"); + } + + return; +} + +void help(char* av[]) { + fprintf(stdout,"ONETIMEBLUH USAGE: \n" + "%s [COMMAND] [OPTIONS] \n \n" + "COMMANDS: \n" + "-d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling \n" + "-e, --encrypt=FILE encrypt message (input) \n" + "-h, --help print this help \n" + "-g, --key-gen create key file \n \n" + "OPTIONS \n" + "-k, --key-file=KEY_FILE use key (input) \n" + "-b, --nbytes=NUM number of bytes \n" + "-o, --output=FILE output name \n" + "-t, --tear-page deletes from the pad file the bytes used to encrypt/decrypt \n \n" + "Onetimebluh project repository at https://git.eigenlab.org/Seppia/onetimebluh \n", av[0]); + + return; +} -- cgit v1.2.3