From e86c2e2058aba0cb96eb4175802b260a5ead3da3 Mon Sep 17 00:00:00 2001 From: Seppia Date: Wed, 30 Nov 2016 01:52:02 +0100 Subject: added the bluh function to transform the message in its bintary dump and overall variable names fixes --- src/onetimebluh.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 116 insertions(+), 10 deletions(-) diff --git a/src/onetimebluh.c b/src/onetimebluh.c index 0823f4d..0d3d324 100644 --- a/src/onetimebluh.c +++ b/src/onetimebluh.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -8,6 +9,7 @@ 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 bluh(int c, char* mess, char* outp); /* performs the binary dump of the input file and prints that to outp */ void help(char* av[]); /* prints the help message */ int main(int argc, char* argv[]) { @@ -27,6 +29,7 @@ int main(int argc, char* argv[]) { while (opt) { int option_index = 0; static struct option options[] = { + {"bluh", required_argument, 0, 'b'}, {"decrypt", required_argument, 0, 'd'}, {"encrypt", required_argument, 0, 'e'}, {"key-file", required_argument, 0, 'k'}, @@ -35,24 +38,33 @@ int main(int argc, char* argv[]) { {"nbytes", required_argument, 0, 'n'}, {"output", required_argument, 0, 'o'}, {"tear-page", no_argument, 0, 't'}, + {"uhbl", required_argument, 0, 'u'}, {0, 0, 0, 0}, }; - if ((opt = getopt_long(argc, argv, "d:e:ghk:n:o:t", options, &option_index)) == -1) + if ((opt = getopt_long(argc, argv, "b:d:e:ghk:n:o:tu:", options, &option_index)) == -1) break; switch (opt) { + case 'b': + message = argv[optind-1]; + if (access(message, F_OK) == -1) { /* look at next comment */ + error(errno, errno, message); + } + command ++; + comm = 'b'; + break; case 'd': message = argv[optind-1]; - command++; - comm = 'u'; + command ++; + comm = 'r'; 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++; + command ++; comm = 'e'; break; case 'g': @@ -61,11 +73,11 @@ int main(int argc, char* argv[]) { break; case 'h': help(argv); - command++; + command ++; break; case 'k': keyfile = argv[optind-1]; - if (access(keyfile, F_OK) == -1) { /* look at the comment before */ + if (access(keyfile, F_OK) == -1) { /* look at the previous comment */ error(errno, errno, keyfile); } break; @@ -78,6 +90,14 @@ int main(int argc, char* argv[]) { case 't': tear = 1; break; + case 'u': + message = argv[optind-1]; + if (access(message, F_OK) == -1) { /* just guess */ + error(errno, errno, message); + } + command ++; + comm = 'u'; + break; case '?': break; default: @@ -103,7 +123,7 @@ int main(int argc, char* argv[]) { /* Next section detects the functions to call */ - if (comm == 'e' || comm == 'u') { + if (comm == 'e' || comm == 'r') { if (keyfile == NULL) { printf("No key specified: exit! \n"); exit(EXIT_FAILURE); @@ -114,6 +134,8 @@ int main(int argc, char* argv[]) { xor(comm, message, keyfile, output); } else if (comm == 'g') { keyrand(nbytes, output); + } else if (comm == 'b' || comm == 'u') { + bluh(comm, message, output); } exit(EXIT_SUCCESS); @@ -124,13 +146,13 @@ 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 */ + /* In absence of input by users next 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') { + } else if (ed == 'r' || ed == 's') { printf("WARNING no output name specified usign default value 'decritt' \n"); outp = defdeoutp; } @@ -163,7 +185,7 @@ void xor(int ed, char* mess, char* keyf, char* outp) { fwrite(bytes, sizeof(char), mess_size, critt); - if (ed == 'f' || ed == 'v') { + if (ed == 'f' || ed == 's') { ftruncate(fileno(keyx), (pad_size - mess_size)); fseek(keyx, 0L, SEEK_END); long new_pad_size = ftell(keyx); @@ -228,6 +250,90 @@ void keyrand(int nb, char* outp) { return; } +void bluh(int c, char* mess, char* outp) { + + char* defbluh = "bluhed"; + char* defunbluh = "unbluhed"; + + /* In absence of input by users next block sets the default values */ + + if (outp == NULL) { + if (c == 'b') { + printf("WARNING no output name specified using default value 'bluhed' \n"); + outp = defbluh; + } else if (c == 'u') { + printf("WARNING no output name specified usign default value 'unbluhed' \n"); + outp = defunbluh; + } + } + + FILE* mex = fopen(mess, "r"); + FILE* bluh = fopen(outp, "w"); + + long len; + + fseek(mex, 0L, SEEK_END); + len = ftell(mex); + rewind(mex); + + if (c == 'b') { + char* bytes = malloc(len); + char* bits = malloc(8 * len); + + fread(bytes, sizeof(char), len, mex); + + int i,j; + + for (i = 0; i < len; i ++) { + for (j = 0; j < 8; j ++) { + bits[8 * i + j] = (bytes[i] & (1 << (7 -j))) ? '1' : '0'; + printf("%c", bits[8 * i + j]); + } + + /* printf(" "); + if ((i + 1) % 8 == 0) { + printf("\n"); + }*/ + } + + fwrite(bits, sizeof(char), (8 * len), bluh); + free(bytes); + free(bits); + + printf("Message successfully bluhed!\n"); + + } else if (c == 'u') { + char* comp = malloc(len + 1); + char* bits = malloc(8 * len); + + fread(bits, sizeof(char), 8 * len, mex); + memset(comp, 0, (len + 1)); + + int i, j; + + for (i = 0; i < len; i ++) { + for (j = 0; j < 8; j ++) { + if (bits[8 * i + j] == '1') { + comp[i] |= 1 << (7 - j); + } + } + } + + printf("\nOriginal text:\n'%s'\n", comp); + + fwrite(comp, sizeof(char), (len / 8), bluh); + free(comp); + free(bits); + + printf("Message successfully unbluhed!\n"); + } + + fclose(mex); + fclose(bluh); + + return; +} + void help(char* av[]) { fprintf(stdout,"ONETIMEBLUH USAGE: \n" "%s [COMMAND] [OPTIONS] \n \n" -- cgit v1.2.3