From 5ebe0bcd00c8ad70f28fdaa5081459717f56ef02 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Tue, 8 Mar 2022 19:35:24 +0100 Subject: [PATCH] created working decrypter --- src/decrypter.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/src/decrypter.c b/src/decrypter.c index 7a80f3f..cbb17c4 100644 --- a/src/decrypter.c +++ b/src/decrypter.c @@ -1,12 +1,96 @@ #include "../include/decrypter.h" #include +#include #include char *decryptText(char *text, char *key) { - printf("fuk of\n"); + //CONSTS + const int KEY_LENGTH = strlen(key); - exit(0); - return NULL; + //VARIABLES + char *returningText; + int numberBuffer; + char *textBuffer; + + numberBuffer = 1; + + //GET LENTGH OF returningText AND textKeyChain + for (int i = 0; i < strlen(text); i++) + { + if (text[i] == '.') numberBuffer++; + } + + //SET LENGTH + returningText = malloc(numberBuffer); + int textKeyChain[numberBuffer]; + int encryptedTextKeyChain[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]; + } + } + + //LOAD encryptedTextKeyChain + for (int i = 0; i < (sizeof(encryptedTextKeyChain) / sizeof(int)); i++) + { + numberBuffer = 0; + + //GET LENGTH OF EACH CHARACTER + for (int j = 0; j < strlen(text); j++) + { + if (text[j] == '.') break; + + numberBuffer++; + } + + textBuffer = malloc(numberBuffer); + + //LOAD textBuffer + for (int j = 0; j < strlen(text); j++) + { + textBuffer[j] = text[j]; + + if (numberBuffer == j) break; + } + + encryptedTextKeyChain[i] = atoi(textBuffer); + + text += numberBuffer + 1; + free(textBuffer); + } + + //DECRYPT TEXT + for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) + { + textKeyChain[i] -= encryptedTextKeyChain[i]; + } + + //LOAD returningText + for (int i = 0; i < sizeof(textKeyChain) / sizeof(int); i++) + { + returningText[i] = (char) textKeyChain[i]; + } + + return returningText; } \ No newline at end of file