From 7d5ea469a8291062dbc6a8bbf9b3ca8942d41a21 Mon Sep 17 00:00:00 2001 From: Seppia Date: Sun, 12 Jun 2016 17:08:18 +0200 Subject: added decrypt option just for the feeling --- onetimebluh.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/onetimebluh.c b/onetimebluh.c index e6366f0..3b163c3 100644 --- a/onetimebluh.c +++ b/onetimebluh.c @@ -6,7 +6,7 @@ #include #include -void xor(char* mess, char* keyf, char* outp); +void xor(int ed, char* mess, char* keyf, char* outp); void keyrand(int nb, char* outp); void help(char* av[]); @@ -22,6 +22,7 @@ int main(int argc, char* argv[]) { 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'}, @@ -31,13 +32,18 @@ int main(int argc, char* argv[]) { {0, 0, 0, 0}, }; - if ((opt = getopt_long(argc, argv, "b:e:ghk:o:", options, &option_index)) == -1) + if ((opt = getopt_long(argc, argv, "b:d:e:ghk:o:", options, &option_index)) == -1) break; switch (opt) { case 'b': nbytes = atoi(argv[optind-1]); break; + case 'd': + message = argv[optind-1]; + command++; + comm = 'd'; + break; case 'e': message = argv[optind-1]; command++; @@ -77,12 +83,12 @@ int main(int argc, char* argv[]) { exit(EXIT_FAILURE); } - if (comm == 'e') { + if (comm == 'e' || comm == 'd') { if (keyfile == NULL) { printf("No key specified: exit! \n"); exit(EXIT_FAILURE); } - xor(message, keyfile, output); + xor(comm, message, keyfile, output); } else if (comm == 'g') { keyrand(nbytes, output); } @@ -90,9 +96,10 @@ int main(int argc, char* argv[]) { exit(EXIT_SUCCESS); } -void xor(char* mess, char* keyf, char* outp) { +void xor(int ed, char* mess, char* keyf, char* outp) { - char* defoutp = "critt"; + char* defenoutp = "critt"; + char* defdeoutp = "decritt"; if (access(mess, F_OK) == -1) { error(errno, errno, mess); @@ -101,8 +108,13 @@ void xor(char* mess, char* keyf, char* outp) { } if (outp == NULL) { - printf("WARNING no output name specified using default value 'critt' \n"); - outp = defoutp; + if (ed == 'e') { + printf("WARNING no output name specified using default value 'critt' \n"); + outp = defenoutp; + } else if (ed == 'd') { + printf("WARNING no output name specified usign default value 'decritt' \n"); + outp = defdeoutp; + } } FILE* mex = fopen(mess, "r"); FILE* keyx = fopen(keyf, "r"); @@ -122,7 +134,11 @@ void xor(char* mess, char* keyf, char* outp) { fclose(keyx); fclose(critt); - printf("Message successfully encrypted \n"); + if (ed == 'e') { + printf("Message successfully encrypted \n"); + } else if (ed == 'd') { + printf("Message successfully decrypted \n"); + } return; } @@ -167,6 +183,7 @@ void help(char* av[]) { printf("ONETIMEBLUH USAGE: \n"); printf("%s [COMMAND] [OPTIONS] \n \n", av[0]); printf("COMMANDS: \n"); + printf("-d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling \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"); -- cgit v1.2.3