summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSeppia <nonso@insicuri.net>2016-06-15 15:32:25 +0200
committerSeppia <nonso@insicuri.net>2016-06-15 15:32:25 +0200
commit068ef11b4b0b42ed72e0a215b7745aadbd970640 (patch)
tree7484d81896b0e36cb7e46d1df553d81340b89cd5
parent18cfb98e6fdb9e5398527bde6b0fbe0186c760f3 (diff)
downloadonetimebluh-068ef11b4b0b42ed72e0a215b7745aadbd970640.tar.gz
onetimebluh-068ef11b4b0b42ed72e0a215b7745aadbd970640.tar.bz2
onetimebluh-068ef11b4b0b42ed72e0a215b7745aadbd970640.zip
added option to delete used key bytes
-rw-r--r--onetimebluh.c72
1 files changed, 51 insertions, 21 deletions
diff --git a/onetimebluh.c b/onetimebluh.c
index 10cbca4..8c46524 100644
--- a/onetimebluh.c
+++ b/onetimebluh.c
@@ -15,6 +15,7 @@ int main(int argc, char* argv[]) {
int opt = 1;
int command = 0;
int comm = 0;
+ int tear = 0;
char* message = NULL;
char* keyfile = NULL;
char* output = NULL;
@@ -33,10 +34,11 @@ int main(int argc, char* argv[]) {
{"help", no_argument, 0, 'h'},
{"nbytes", required_argument, 0, 'b'},
{"output", required_argument, 0, 'o'},
+ {"tear-page", no_argument, 0, 't'},
{0, 0, 0, 0},
};
- if ((opt = getopt_long(argc, argv, "b:d:e:ghk:o:", options, &option_index)) == -1)
+ if ((opt = getopt_long(argc, argv, "b:d:e:ghk:o:t", options, &option_index)) == -1)
break;
switch (opt) {
@@ -46,7 +48,7 @@ int main(int argc, char* argv[]) {
case 'd':
message = argv[optind-1];
command++;
- comm = 'd';
+ comm = 'u';
break;
case 'e':
message = argv[optind-1];
@@ -73,6 +75,9 @@ int main(int argc, char* argv[]) {
case 'o':
output = argv[optind-1];
break;
+ case 't':
+ tear = 1;
+ break;
case '?':
break;
default:
@@ -98,11 +103,14 @@ int main(int argc, char* argv[]) {
/* Next section detects the functions to call */
- if (comm == 'e' || comm == 'd') {
+ if (comm == 'e' || comm == 'u') {
if (keyfile == NULL) {
printf("No key specified: exit! \n");
exit(EXIT_FAILURE);
}
+ if (tear == 1) {
+ comm++;
+ }
xor(comm, message, keyfile, output);
} else if (comm == 'g') {
keyrand(nbytes, output);
@@ -119,22 +127,35 @@ void xor(int ed, char* mess, char* keyf, char* outp) {
/* In absence of input by users nex block sets the default values */
if (outp == NULL) {
- if (ed == 'e') {
+ if (ed == 'e' || ed == 'f') {
printf("WARNING no output name specified using default value 'critt' \n");
outp = defenoutp;
- } else if (ed == 'd') {
+ } else if (ed == 'u' || ed == 'v') {
printf("WARNING no output name specified usign default value 'decritt' \n");
outp = defdeoutp;
}
}
FILE* mex = fopen(mess, "r");
- FILE* keyx = fopen(keyf, "r");
+ FILE* keyx = fopen(keyf, "r+");
FILE* critt = fopen(outp, "w");
+
+ long mess_size;
+ long pad_size;
+
+ if (ed == 'f' || ed == 'v') {
+ fseek(mex, 0L, SEEK_END);
+ mess_size = ftell(mex);
+ rewind(mex);
+ fseek(keyx, 0L, SEEK_END);
+ pad_size = ftell(keyx);
+ fseek(keyx, (pad_size - mess_size), SEEK_SET);
+ }
+
int i = 1;
char a, b;
- while(i != EOF) {
+ while (i != EOF) {
i = fscanf(mex, "%c", &a);
fscanf(keyx, "%c", &b);
if(i != EOF) {
@@ -142,13 +163,21 @@ void xor(int ed, char* mess, char* keyf, char* outp) {
}
}
+ if (ed == 'f' || ed == 'v') {
+ ftruncate(fileno(keyx), (pad_size - mess_size));
+ fseek(keyx, 0L, SEEK_END);
+ long new_pad_size = ftell(keyx);
+ printf("Your pad is now %li bytes shorter \n", mess_size);
+ printf("You now have %li bytes left \n", new_pad_size);
+ }
+
fclose(mex);
fclose(keyx);
fclose(critt);
- if (ed == 'e') {
+ if (ed == 'e' || ed == 'f') {
printf("Message successfully encrypted \n");
- } else if (ed == 'd') {
+ } else if (ed == 'u' || ed == 'v') {
printf("Message successfully decrypted \n");
}
@@ -197,18 +226,19 @@ void keyrand(int nb, char* outp) {
}
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");
- printf("OPTIONS \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 https://git.eigenlab.org/Seppia/onetimebluh \n");
+ fprintf(stdout,"ONETIMEBLUH USAGE: \n"
+ "%s [COMMAND] [OPTIONS] \n \n"
+ "COMMANDS: \n"
+ "-d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling \n"
+ "-e, --encrypt=FILE encrypt message (input) \n"
+ "-h, --help print this help \n"
+ "-g, --key-gen create key file \n \n"
+ "OPTIONS \n"
+ "-k, --key-file=KEY_FILE use key (input) \n"
+ "-b, --nbytes=NUM number of bytes \n"
+ "-o, --output=FILE output name \n"
+ "-t, --tear-page deletes from the pad file the bytes used to encrypt/decrypt \n \n"
+ "Onetimebluh project repository at https://git.eigenlab.org/Seppia/onetimebluh \n", av[0]);
return;
}