From 6fb039da2b18c278817dfcdb240e3e5032902e0a Mon Sep 17 00:00:00 2001 From: Seppia Date: Sun, 12 Jun 2016 05:30:21 +0200 Subject: major code fix in ontimebluh.c 'comments coming soon' --- onetimebluh.c | 132 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 39 deletions(-) diff --git a/onetimebluh.c b/onetimebluh.c index 83cf93a..98d0d3b 100644 --- a/onetimebluh.c +++ b/onetimebluh.c @@ -1,64 +1,115 @@ #include #include -#include +#include -void xor(int ac, char* av[]); +void xor(char* mess, char* keyf, char* outp); void help(char* av[]); int main(int argc, char* argv[]) { - if (atoi(argv[1]) == 1){ - xor(argc, argv); - printf("Message successfully encrypted! \n"); - } else if (atoi(argv[1]) == 2) { - printf("Working \n"); - } else { - printf("WARNING no command specified!! \n"); - help(argv); - fprintf(stderr,"Exiting... \n"); - exit(EXIT_FAILURE); - } - - exit(EXIT_SUCCESS); -} + int opt = 1; + int command = 0; + int comm = 0; + char* message = NULL; + char* keyfile = NULL; + char* output = NULL; + while (opt) { + int option_index = 0; + static struct option options[] = { + {"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'}, + {0, 0, 0, 0}, + }; -void xor(int ac, char* av[]) { + if ((opt = getopt_long(argc, argv, "b:e:ghk:o:", options, &option_index)) == -1) + break; - int opt; - char* defname = "critt"; - int defout = 0; - char* output; - while ((opt = getopt(ac, av, "o:")) != -1) { switch (opt) { + case 'b': + printf("Work in progress \n"); + break; + case 'e': + message = argv[optind-1]; + command++; + comm = 'e'; + break; + case 'g': + printf("Work in progress \n"); + command++; + comm = 'g'; + break; + case 'h': + help(argv); + command++; + break; + case 'k': + keyfile = argv[optind-1]; + break; case 'o': - output = av[(optind-1)]; - defout = 1; + output = argv[optind-1]; + break; + case '?': break; default: - fprintf(stderr,"Usage %s %s [-o output] \n", av[0], av[1]); + printf("carachter code returned 0%o \n", opt); + } + } + + if (command == 0) { + printf("No command called \n"); + exit(EXIT_FAILURE); + } else if (command > 1) { + printf("Multiple commands called (print usage)\n"); + exit(EXIT_FAILURE); + } + + if (optind < argc) { + printf("Too many arguments \n"); + exit(EXIT_FAILURE); + } + + if (comm == 'e') { + if (keyfile == NULL) { + printf("No key specified: exit! \n"); exit(EXIT_FAILURE); } + xor(message, keyfile, output); } - if (defout == 0) { - printf("WARNING no command specified using default value critt \n"); - output = defname; + exit(EXIT_SUCCESS); +} + +void xor(char* mess, char* keyf, char* outp) { + + char* defoutp = "critt"; + + if (outp == NULL) { + printf("WARNING no output name specified using default value 'critt' \n"); + outp = defoutp; } - FILE* mess_uno = fopen(av[2], "r"); - FILE* mess_due = fopen(av[3], "r"); - FILE* critt = fopen(output, "w"); + FILE* mex = fopen(mess, "r"); + FILE* keyx = fopen(keyf, "r"); + FILE* critt = fopen(outp, "w"); int i = 1; char a, b; + while(i != EOF) { - i = fscanf(mess_uno, "%c", &a); - fscanf(mess_due, "%c", &b); + i = fscanf(mex, "%c", &a); + fscanf(keyx, "%c", &b); if(i != EOF) { fprintf(critt, "%c", a^b); } } - fclose(mess_uno); - fclose(mess_due); + + fclose(mex); + fclose(keyx); fclose(critt); + printf("Message successfully encrypted \n"); + return; } @@ -66,11 +117,14 @@ void help(char* av[]) { printf("ONETIMEBLUH USAGE: \n"); printf("%s [COMMAND] [OPTIONS] \n \n", av[0]); printf("COMMANDS: \n"); - printf("1 -> encrypt encrypt message (argv[1]) using key (argv[2]) \n"); - printf("2 -> keygen create key file \n \n"); + printf("-e, --encrypt=FILE encrypt message (input) \n"); + printf("-h, --help print this help \n"); + printf("-g, --key-gen create key file \n \n"); printf("OPTIONS \n"); - printf("-b number of bytes \n"); - printf("-o output name \n"); + printf("-k, --key-file=KEY_FILE use key (input) \n"); + printf("-b, --nbytes=NUM number of bytes \n"); + printf("-o, --output=FILE output name \n \n"); + printf("Onetimebluh project repository at http://git.eigenlab.org/Seppia/onetimebluh \n"); return; } -- cgit v1.2.3