...
 
Commits (2)
  • Seppia's avatar
    encrypt/decrypt functions · 35748b20
    Seppia authored
    The encrypt and decrypt operations have been moved to functions that
    take input from string or file, and corrispondent code has been
    changed accordingly.
    35748b20
  • Seppia's avatar
    Missing string memory deallocation · babf2ff2
    Seppia authored
    Adds missing free function for dinamically allocated string and also
    moves a variable declaration to the appropriate block.
    babf2ff2
......@@ -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);
......@@ -325,8 +357,9 @@ void keyrand (Opts* opzioni) {
unsigned char* key = malloc (opzioni->nbytes);
RAND_bytes (key, opzioni->nbytes);
FILE* file;
if (opzioni->output) {
FILE* file;
file = fopen (opzioni->output, "w");
fwrite (key, sizeof(char), opzioni->nbytes, file);
fclose (file);
......@@ -341,6 +374,7 @@ void keyrand (Opts* opzioni) {
}
fprintf (stdout, "%s", key);
free (key);
if (!opzioni->quiet) {
printf ("\n");
......