From 8ad3e917a43c040df606cfb25ab5c0a3eb91f596 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 27 Feb 2022 20:34:52 -0500 Subject: [PATCH] first commit --- .gitignore | 1 + Makefile | 18 ++++++ cmd/strenc.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++ include/strenc.h | 9 +++ 4 files changed, 177 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 cmd/strenc.c create mode 100644 include/strenc.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c9036e8 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +LIBRARIES = -Iinclude +SOURCES = ./cmd/strenc.c +OUTPUT_DIR = ./bin +OUTPUT = -o ${OUTPUT_DIR}/PROG +INSTALL_OUTPUT = ${OUTPUT_DIR}/PROG + +build: output_dir + gcc -Wall ${SOURCES} ${OUTPUT:PROG=strenc} ${LIBRARIES} + +debug: output_dir + gcc -Wall -g ${SOURCES} ${OUTPUT:PROG=strenc} ${LIBRARIES} + +output_dir: + mkdir -p ${OUTPUT_DIR} + +clean: + rm -rf $(OUTPUT_DIR) ${CERT_DIR} **.h.gch + diff --git a/cmd/strenc.c b/cmd/strenc.c new file mode 100644 index 0000000..d506d73 --- /dev/null +++ b/cmd/strenc.c @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include + +// Local includes +#include "strenc.h" + +// Open file and return point to char containing file contents +unsigned char *openFile(char *fileName) { + size_t c = 0; + size_t size = 8; + unsigned char *buf = malloc(size); + + FILE *fp; + fp = fopen(fileName, "r"); + + for (size_t i = 0; (c = fgetc(fp)) != EOF; i++) { + if (i == size) { // If size of current buffer need to be doubled + size *= 2; + buf = reallocarray(buf, size, sizeof(unsigned char)); + if (buf == NULL) { + printf("Error resizing array, exiting...\n"); + exit(EXIT_FAILURE); + } + } + buf[i] = c; + } + + fclose(fp); + return buf; +} + +unsigned char *genKey(unsigned char *pt) { + unsigned char *key = malloc(strlen((char *)pt)); + srand(time(NULL)); + + // rand() should not normally be used for + // generating secure numbers, we do not care + // here + for (int i = 0; i < strlen((char *)pt); i++) { + key[i] = rand(); + } + + return key; +} + +unsigned char *xorCharArray(unsigned char *text, unsigned char *key) { + unsigned char *ft = malloc(strlen((char *)text)); + + for (int i = 0; i < strlen((char *)text); i++) { // Xor pt & key + ft[i] = text[i] ^ key[i]; + } + + return ft; +} + +int printCStyleCharArray(unsigned char *text, unsigned char *key) { + // Print text + printf("unsigned char text[%zu] = { ", strlen((char *)text)); + for (int i = 0; i < strlen((char *)text); i++) { + printf("0x%02X", text[i]); + if ((i + 1) == strlen((char *)text)) { + printf(" "); + } else { + printf(", "); + } + } + printf("};\n"); + // Print key + printf("unsigned char text_key[%zu] = { ", strlen((char *)key)); + for (int i = 0; i < strlen((char *)key); i++) { + printf("0x%02X", key[i]); + if ((i + 1) == strlen((char *)key)) { + printf(" "); + } else { + printf(", "); + } + } + printf("};\n"); + return 0; +} + +int main(int argc, char **argv) { + unsigned char *pt = malloc(sizeof(unsigned char)); + unsigned char *key = malloc(sizeof(unsigned char)); + unsigned char *ct = malloc(sizeof(unsigned char)); + + // Setting up options + static const struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"file", required_argument, NULL, 'f'}, + {"string", required_argument, NULL, 's'}, + {0, 0, 0, 0} + }; + + const char* usage = + "Usage strenc [options]\n\n" + " -h --help\t\t\tShows this message\n" + " -f --file\t\t\tInput file to be xored\n" + " -s --string\t\t\tInput string to be xored\n" + "\n"; + + int c; // Used to grab individual args + while(1) { + int option_index = 0; + c = getopt_long(argc, argv, "hf:s:", long_options, &option_index); + if (c == -1) { // Break if no more args are present + break; + } + switch(c) { + case 'h': + printf("%s", usage); + exit(EXIT_SUCCESS); + case 'f': + if (strlen((char *)pt) != 0) { // Exit if string is already set + printf("Error string input already set\n"); + exit(EXIT_FAILURE); + } + pt = openFile(optarg); + break; + case 's': + if (strlen((char *)pt) != 0) { // Exit if file is already set + printf("Error file input already set\n"); + exit(EXIT_FAILURE); + } + pt = malloc(strlen(optarg)); + strcpy((char *)pt, optarg); + break; + } + } + if (strlen((char *)pt) == 0) { // Exit if plain text is not > len 0 + printf("Plain text has not been set\n"); + exit(EXIT_FAILURE); + } + + key = genKey(pt); + ct = xorCharArray(pt, key); + + printCStyleCharArray(ct, key); + + // Freeing memory and returning + free(pt); + free(key); + free(ct); + exit(EXIT_SUCCESS); +} + diff --git a/include/strenc.h b/include/strenc.h new file mode 100644 index 0000000..67a312e --- /dev/null +++ b/include/strenc.h @@ -0,0 +1,9 @@ +#include +#include + +unsigned char *openFile(char *fileName); +unsigned char *genKey(unsigned char *pt); +unsigned char *xorCharArray(unsigned char *text, unsigned char *key); +int printCStyleCharArray(unsigned char *text, unsigned char *key); +int main(int argc, char **argv); +