From 61ce5d0c1babd239d29b690b5b616800dccaa138 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Tue, 3 May 2022 18:37:28 +0200 Subject: [PATCH] implemented keyLength --- src/decrypter.c | 9 ++++----- src/encrypter.c | 13 ++++++------- src/misc.c | 6 +++--- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/decrypter.c b/src/decrypter.c index 5e4f0dd..98f3bd5 100644 --- a/src/decrypter.c +++ b/src/decrypter.c @@ -11,15 +11,14 @@ char* decryptText(char *text, char *key) { //CHECK FOR INVALID key - if (strlen(key) < KEY_LENGTH) + if (strlen(key) < getKeyLength()) { - fprintf(stderr, "Key must be at least %d characters long!\n", KEY_LENGTH); + fprintf(stderr, "Key must be at least %d characters long!\n", getKeyLength()); exit(INVALID_KEY); } - //REDEFINE KEY_LENGTH - #undef KEY_LENGTH - #define KEY_LENGTH strlen(key) + //REDEFINE keyLength + setKeyLength(strlen(key)); //VARIABLES char *returningText; diff --git a/src/encrypter.c b/src/encrypter.c index ec060d2..e18dcda 100644 --- a/src/encrypter.c +++ b/src/encrypter.c @@ -18,7 +18,7 @@ encryptText(char *text, char *keyNew) srand(time(0)); //TRY TO MAKE RANDOM GENERATION REALLY "RANDOM" //VARIABLES - char *key = malloc(KEY_LENGTH); + char *key = malloc(getKeyLength()); char *returningText; char *textBuffer; int *textKeyChain = malloc(strlen(text) * sizeof(int)); @@ -26,23 +26,22 @@ encryptText(char *text, char *keyNew) if (keyNew != NULL) { - if (strlen(keyNew) < KEY_LENGTH) + if (strlen(keyNew) < getKeyLength()) { - fprintf(stderr, "Key must be at least %d characters long!\n", KEY_LENGTH); + fprintf(stderr, "Key must be at least %d characters long!\n", getKeyLength()); exit(INVALID_KEY); } strcpy(key, keyNew); - //REDEFINE KEY_LENGTH - #undef KEY_LENGTH - #define KEY_LENGTH strlen(key) + //REDEFINE keyLength + setKeyLength(strlen(key)); goto skipKey; } //LOAD KEY - for (int i = 0; i < KEY_LENGTH; i++) + for (int i = 0; i < getKeyLength(); i++) { //SET numberBuffer TO RANDOM NUMBER BETWEEN 0 AND 52 numberBuffer = (rand() % 52) + 1; diff --git a/src/misc.c b/src/misc.c index 85009e4..8ac5f03 100644 --- a/src/misc.c +++ b/src/misc.c @@ -103,10 +103,10 @@ generateTextKeyChain(char key[], int *textKeyChain, int textKeyChainSize) { numberBuffer = i; - //CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH - while (numberBuffer >= KEY_LENGTH) + //CHECK, IF numberBuffer ISN'T GREATER THAN keyLength AND CUT UNUSED LENGTH + while (numberBuffer >= getKeyLength()) { - numberBuffer -= KEY_LENGTH; + numberBuffer -= getKeyLength(); } //FILL textKeyChain