From 35748b207e9b32e59fb391d25d9fe8b4da403855 Mon Sep 17 00:00:00 2001 From: Seppia Date: Thu, 5 Jul 2018 16:17:55 +0200 Subject: encrypt/decrypt functions The encrypt and decrypt operations have been moved to functions that take input from string or file, and corrispondent code has been changed accordingly. --- src/onetimebluh.c | 70 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/onetimebluh.c b/src/onetimebluh.c index 08f147d..dce4a6b 100644 --- a/src/onetimebluh.c +++ b/src/onetimebluh.c @@ -19,6 +19,8 @@ typedef struct opts { } Opts; char* readInput (FILE* input, uint64_t size); +char* cryptXor (char* inputStr, FILE* keyFile); +char* fcryptXor (FILE* inputFile, FILE* keyFile); void xor (Opts* opzioni); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ void keyrand (Opts* opzioni); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ void bluh (Opts* opzioni); /* performs the binary dump of the input file and prints that to outp */ @@ -198,6 +200,50 @@ char* readInput (FILE* stdinput, uint64_t size) { return realloc (input, sizeof (char) * length); } +char* cryptXor (char* inputStr, FILE* keyFile) { + + int64_t i; + char in1, in2; + int64_t inputSize = strlen (inputStr); + char* bytes = malloc (inputSize); + + fseek (keyFile, 0L, SEEK_END); + int64_t keySize = ftell (keyFile); + fseek (keyFile, (keySize - inputSize), SEEK_SET); + + for (i = 0; i < inputSize; i ++) { + in1 = inputStr[i]; + fscanf (keyFile, "%c", &in2); + bytes[i] = in1 ^ in2; + } + + return bytes; +} + +char* fcryptXor (FILE* inputFile, FILE* keyFile) { + + int64_t i; + char in1, in2; + + fseek (inputFile, 0L, SEEK_END); + int64_t inputSize = ftell (inputFile); + rewind (inputFile); + + char* bytes = malloc (inputSize); + + fseek (keyFile, 0L, SEEK_END); + int64_t keySize = ftell (keyFile); + fseek (keyFile, (keySize - inputSize), SEEK_SET); + + for (i = 0; i < inputSize; i ++) { + fscanf (inputFile, "%c", &in1); + fscanf (keyFile, "%c", &in2); + bytes[i] = in1 ^ in2; + } + + return bytes; +} + void xor (Opts* opzioni) { char* input; @@ -219,7 +265,6 @@ void xor (Opts* opzioni) { } int64_t mess_size; - int64_t pad_size; if (opzioni->input) { fseek (mex, 0L, SEEK_END); @@ -229,27 +274,12 @@ void xor (Opts* opzioni) { mess_size = strlen (input); } - fseek (keyx, 0L, SEEK_END); - pad_size = ftell (keyx); - fseek (keyx, (pad_size - mess_size), SEEK_SET); - - char* bytes = malloc (mess_size); - - int64_t i; - char a, b; + char* bytes; if (opzioni->input) { - for (i = 0; i < mess_size; i ++) { - fscanf (mex, "%c", &a); - fscanf (keyx, "%c", &b); - bytes[i] = a ^ b; - } + bytes = fcryptXor (mex, keyx); } else if (!opzioni->input) { - for (i = 0; i < mess_size; i ++) { - a = input[i]; - fscanf (keyx, "%c", &b); - bytes[i] = a ^ b; - } + bytes = cryptXor (input, keyx); } if (opzioni->output) { @@ -268,6 +298,8 @@ void xor (Opts* opzioni) { if (opzioni->tear) { + fseek (keyx, 0L, SEEK_END); + int64_t pad_size = ftell (keyx); ftruncate (fileno (keyx), (pad_size - mess_size)); fseek (keyx, 0L, SEEK_END); int64_t new_pad_size = ftell (keyx); -- cgit v1.2.3