diff options
author | Seppia <nonso@insicuri.net> | 2016-06-12 18:01:59 +0200 |
---|---|---|
committer | Seppia <nonso@insicuri.net> | 2016-06-12 18:01:59 +0200 |
commit | da5ade6e929a8c343d821847ffd361f231f01dc5 (patch) | |
tree | 23619707d8fa4fd6854444ef8e4b60ef57e931f6 | |
parent | 91facf3cb62d5f8b102d2f567f82b4d5c4b01f74 (diff) | |
parent | e43d3106afbead6dcfc172b0edc037bb2d5ebb83 (diff) | |
download | onetimebluh-da5ade6e929a8c343d821847ffd361f231f01dc5.tar.gz onetimebluh-da5ade6e929a8c343d821847ffd361f231f01dc5.tar.bz2 onetimebluh-da5ade6e929a8c343d821847ffd361f231f01dc5.zip |
Merge branch 'experimental'
-rw-r--r-- | README.md | 41 | ||||
-rw-r--r-- | keygen.c | 64 | ||||
-rw-r--r-- | onetimebluh.c | 199 |
3 files changed, 282 insertions, 22 deletions
@@ -1 +1,40 @@ -Blblblblbl +Onetimebluh (Workbluhp in blogress!!) +====================================== + +Simple implementation of One Time Pad cipher. +Capable of generating cryptographically strong random number keys and of usign them to encrypt/decrypt messages of same length with a bitwise XOR function. + +Usage +------- + +Example creates two keys of lenght 4096 bytes and use the first as a message and the second as the pad key (just for testing): + +``` +onetimebluh --key-gen --nbytes 4096 --output message +onetimebluh --key-gen --nbytes 4096 --output pad +onetimebluh --encrypt message --key-file key --output encrypted-message +onetimebluh --decrypt encrypted-message --key-file key --outptut decrypted-message +``` + +You can print the help by running `onetimebluh --help` or `onetimebluh -h`. + +``` +ONETIMEBLUH USAGE:; +onetimebluh [COMMAND] [OPTIONS] ... + +COMMANDS: + +-d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling +-e, --encrypt=FILE encrypt message (input) +-h, --help print this help +-g, --key-gen create key file + +OPTIONS + +-k, --key-file=FILE use key (input) +-b, --nbytes=NUM number of bytes +-o, --output=FILE output name + +Onetimebluh project repository at http://git.eigenlab.org/Seppia/onetimebluh + +``` @@ -1,14 +1,60 @@ #include<stdio.h> #include<openssl/rand.h> +#include<unistd.h> int main(int argc, char* argv[]) { - long long int len = strtoll(argv[1], NULL, 10); - unsigned char key[len]; - RAND_bytes(key, len); - FILE* file = fopen(argv[2], "w"); - fwrite(key, len, 1, file); - fclose(file); - printf("Created key file %s of %lli bytes \n", argv[2], len); - - return 0; + + int opt; + int nbytes = 256; + char* defname = "default.key"; + char* output; + int defout = 0; + int defbyte = 0; + int noopts = 0; + while ((opt = getopt(argc, argv, "b:o:")) != -1) { + switch (opt) { + case 'b': + nbytes = atoi(optarg); + defbyte = 1; + break; + case 'o': + output = argv[(optind-1)]; + defout = 1; + break; + default: + fprintf(stderr, "Usage: %s [-b nbytes] [-o output] \n", argv[0]); + exit(EXIT_FAILURE); + } + if (noopts == 0) { + noopts = 1; + } + } + + if(noopts == 0) { + printf("WARNING no option specified usign default values... \n"); + } + if (nbytes < 0) { + printf("Negative byte value inserted! \n"); + printf("Exiting... \n"); + exit(EXIT_FAILURE); + } else if (nbytes != 0) { + if (defbyte == 0) { + printf("No byte number specified... using default value: 256 \n"); + } + unsigned char key[nbytes]; + RAND_bytes(key, nbytes); + if (defout == 0) { + output = defname; + printf("No output name specified... using default value: default.key \n"); + } + FILE* file = fopen(output, "w"); + fwrite(key, nbytes, 1, file); + fclose(file); + printf("Created key file %s of %d bytes \n", output, nbytes); + } else { + printf("Byte number specified is 0. \n"); + printf("Doing nothing! \n"); + } + + exit(EXIT_SUCCESS); } diff --git a/onetimebluh.c b/onetimebluh.c index f838db3..2aafbd6 100644 --- a/onetimebluh.c +++ b/onetimebluh.c @@ -1,22 +1,197 @@ -#include<stdio.h> -#include<stdlib.h> +#include <stdio.h> +#include <stdlib.h> +#include <getopt.h> +#include <openssl/rand.h> +#include <unistd.h> +#include <errno.h> +#include <error.h> + +void xor(int ed, char* mess, char* keyf, char* outp); +void keyrand(int nb, char* outp); +void help(char* av[]); int main(int argc, char* argv[]) { - FILE* uno = fopen(argv[1], "r"); - FILE* due = fopen(argv[2], "r"); - FILE* tre = fopen(argv[3], "w"); + int opt = 1; + int command = 0; + int comm = 0; + char* message = NULL; + char* keyfile = NULL; + char* output = NULL; + int nbytes = -1; // must be resolved temporary workaround (ho sonno) + + 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'}, + {"help", no_argument, 0, 'h'}, + {"nbytes", required_argument, 0, 'b'}, + {"output", required_argument, 0, 'o'}, + {0, 0, 0, 0}, + }; + + 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++; + comm = 'e'; + break; + case 'g': + command++; + comm = 'g'; + break; + case 'h': + help(argv); + command++; + break; + case 'k': + keyfile = argv[optind-1]; + break; + case 'o': + output = argv[optind-1]; + break; + case '?': + break; + default: + printf("carachter code returned 0%o \n", opt); + } + } + + if (command == 0) { + printf("No command called \n"); + exit(EXIT_FAILURE); + } else if (command > 1) { + printf("Multiple commands called (print usage)\n"); + exit(EXIT_FAILURE); + } + + if (optind < argc) { + printf("Too many arguments \n"); + exit(EXIT_FAILURE); + } + + if (comm == 'e' || comm == 'd') { + if (keyfile == NULL) { + printf("No key specified: exit! \n"); + exit(EXIT_FAILURE); + } + xor(comm, message, keyfile, output); + } else if (comm == 'g') { + keyrand(nbytes, output); + } + + exit(EXIT_SUCCESS); +} + +void xor(int ed, char* mess, char* keyf, char* outp) { + + char* defenoutp = "critt"; + char* defdeoutp = "decritt"; + + if (access(mess, F_OK) == -1) { + error(errno, errno, mess); + } else if (access(keyf, F_OK) == -1) { + error(errno, errno, keyf); + } + + if (outp == NULL) { + 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"); + FILE* critt = fopen(outp, "w"); int i = 1; char a, b; + while(i != EOF) { - i = fscanf(uno, "%c", &a); - fscanf(due, "%c", &b); + i = fscanf(mex, "%c", &a); + fscanf(keyx, "%c", &b); if(i != EOF) { - fprintf(tre, "%c", a^b); + fprintf(critt, "%c", a^b); } } - fclose(uno); - fclose(due); - fclose(tre); - return 0; + fclose(mex); + fclose(keyx); + fclose(critt); + + if (ed == 'e') { + printf("Message successfully encrypted \n"); + } else if (ed == 'd') { + printf("Message successfully decrypted \n"); + } + + return; +} + +void keyrand(int nb, char* outp) { + + char* defoutp = "default.key"; + + if((nb == -1) && (outp == NULL)) { + printf("WARNING no option specified usign default values... \n"); + nb = 256; + outp = defoutp; + } + if (nb < -1) { // orribile + printf("Negative byte value inserted! \n"); + printf("Exiting... \n"); + exit(EXIT_FAILURE); + } else if (nb != 0) { + if(nb == -1) { + printf("No byte number specified... using default value: 256 \n"); + nb = 256; + } + unsigned char key[nb]; + RAND_bytes(key, nb); + if (outp == NULL) { + outp = defoutp; + printf("No output name specified... using default value: default.key \n"); + } + FILE* file = fopen(outp, "w"); + fwrite(key, nb, 1, file); + fclose(file); + printf("Created key file %s of %d bytes \n", outp, nb); + } else { + printf("Byte number specified is 0. \n"); + printf("Doing nothing! \n"); + } + + return; +} + +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 http://git.eigenlab.org/Seppia/onetimebluh \n"); + + return; } |