From 4f50c2b4bb8e178b00fd00dc6cfd95140bb94eb0 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 27 Apr 2022 18:16:37 +0200 Subject: [PATCH] implemented generateTextKeyChain --- src/decrypter.c | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/decrypter.c b/src/decrypter.c index a92c3fe..de5f51b 100644 --- a/src/decrypter.c +++ b/src/decrypter.c @@ -5,6 +5,7 @@ #include #include "../include/flags.h" +#include "../include/misc.h" char* decryptText(char *text, char *key) @@ -22,10 +23,9 @@ decryptText(char *text, char *key) //VARIABLES char *returningText; - int numberBuffer; + int numberBuffer = 1; char *textBuffer; - - numberBuffer = 1; + int textKeyChainLength; //GET LENGTH OF returningText AND textKeyChain for (int i = 0; i < strlen(text); i++) @@ -33,34 +33,14 @@ decryptText(char *text, char *key) if (text[i] == ENCRYPTION_SEPARATOR) numberBuffer++; } - //SET LENGTH + //SET LENGTH (numberBuffer) returningText = malloc(numberBuffer); - int textKeyChain[numberBuffer]; + int *textKeyChain = malloc(numberBuffer * sizeof(int)); int encryptedTextKeyChain[numberBuffer]; + textKeyChainLength = numberBuffer; //LOAD textKeyChain - for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) - { - numberBuffer = i; - - //CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH - while (numberBuffer >= KEY_LENGTH) - { - numberBuffer -= KEY_LENGTH; - } - - //FILL textKeyChain - if ((numberBuffer + 1) % 3 == 0) - { - textKeyChain[i] = key[numberBuffer] * key[numberBuffer + 1]; - } else if ((numberBuffer + 1) % 2 == 0) - { - textKeyChain[i] = key[numberBuffer] - key[numberBuffer + 1]; - } else - { - textKeyChain[i] = key[numberBuffer] + key[numberBuffer + 1]; - } - } + generateTextKeyChain(key, textKeyChain, numberBuffer); //LOAD encryptedTextKeyChain for (int i = 0; i < (sizeof(encryptedTextKeyChain) / sizeof(int)); i++) @@ -92,13 +72,13 @@ decryptText(char *text, char *key) } //DECRYPT TEXT - for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) + for (int i = 0; i < textKeyChainLength; i++) { textKeyChain[i] -= encryptedTextKeyChain[i]; } //LOAD returningText - for (int i = 0; i < sizeof(textKeyChain) / sizeof(int); i++) + for (int i = 0; i < textKeyChainLength; i++) { returningText[i] = (char) textKeyChain[i]; }