WHY2/src/lib/decrypter.c

148 lines
3.4 KiB
C
Raw Normal View History

2022-05-01 16:23:07 +02:00
#include <why2/decrypter.h>
2022-03-20 18:12:25 +01:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
2022-03-20 18:12:25 +01:00
2022-05-01 16:23:07 +02:00
#include <why2/flags.h>
#include <why2/misc.h>
2022-03-20 18:12:25 +01:00
outputFlags decryptText(char *text, char *keyNew)
2022-03-20 18:12:25 +01:00
{
//CHECK VARIABLE
int checkExitCode;
//TIME VARIABLES
struct timeval startTime;
struct timeval finishTime;
gettimeofday(&startTime, NULL);
//CHECK FOR ACTIVE VERSION
if ((checkExitCode = checkVersion()) != SUCCESS)
{
return noOutput(checkExitCode);
}
//CHECK FOR INVALID text
if ((checkExitCode = checkText(text)) != SUCCESS)
{
return noOutput(checkExitCode);
}
2022-03-20 18:12:25 +01:00
//CHECK FOR INVALID key
if ((checkExitCode = checkKey(keyNew)) != SUCCESS)
{
return noOutput(checkExitCode);
}
2022-04-26 19:04:43 +02:00
2022-05-03 18:37:28 +02:00
//REDEFINE keyLength
setKeyLength(strlen(keyNew));
2022-04-26 19:04:43 +02:00
2022-03-20 18:12:25 +01:00
//VARIABLES
char *returningText;
2022-04-27 18:16:37 +02:00
int numberBuffer = 1;
2022-03-20 18:12:25 +01:00
char *textBuffer;
2022-04-27 18:16:37 +02:00
int textKeyChainLength;
2022-04-27 18:17:35 +02:00
int *textKeyChain;
char *key = malloc(strlen(keyNew) + 1);
int *encryptedTextKeyChain;
//COPY keyNew TO key
strcpy(key, keyNew);
2022-03-20 18:12:25 +01:00
2022-03-20 18:49:26 +01:00
//GET LENGTH OF returningText AND textKeyChain
for (int i = 0; i < (int) strlen(text); i++)
2022-03-20 18:12:25 +01:00
{
2022-06-18 18:30:06 +02:00
if (text[i] == getEncryptionSeparator()) numberBuffer++;
2022-03-20 18:12:25 +01:00
}
2022-04-27 18:16:37 +02:00
//SET LENGTH (numberBuffer)
returningText = malloc(numberBuffer + 1);
textKeyChain = malloc(sizeof(int) * numberBuffer);
encryptedTextKeyChain = malloc(sizeof(int) * numberBuffer);
2022-04-27 18:16:37 +02:00
textKeyChainLength = numberBuffer;
2022-03-20 18:12:25 +01:00
//LOAD textKeyChain
2022-04-27 18:16:37 +02:00
generateTextKeyChain(key, textKeyChain, numberBuffer);
2022-03-20 18:12:25 +01:00
//LOAD encryptedTextKeyChain
for (int i = 0; i < textKeyChainLength; i++)
2022-03-20 18:12:25 +01:00
{
numberBuffer = 0;
//GET LENGTH OF EACH CHARACTER
for (int j = 0; j < (int) strlen(text); j++)
2022-03-20 18:12:25 +01:00
{
2022-06-18 18:30:06 +02:00
if (text[j] == getEncryptionSeparator()) break;
2022-03-20 18:12:25 +01:00
numberBuffer++;
}
if (i != 0)
{
textBuffer = realloc(textBuffer, numberBuffer + 1);
} else
{
textBuffer = malloc(numberBuffer + 1);
}
2022-03-20 18:12:25 +01:00
//CLEAN (POSSIBLY EXISTING) JUNK in textBuffer
for (int i = 0; i <= numberBuffer; i++)
{
textBuffer[i] = '\0';
}
2022-03-20 18:12:25 +01:00
//LOAD textBuffer
for (int j = 0; j < (int) strlen(text); j++)
2022-03-20 18:12:25 +01:00
{
textBuffer[j] = text[j];
if (numberBuffer == j) break;
}
encryptedTextKeyChain[i] = atoi(textBuffer);
text += numberBuffer + 1;
}
//DECRYPT TEXT
2022-04-27 18:16:37 +02:00
for (int i = 0; i < textKeyChainLength; i++)
2022-03-20 18:12:25 +01:00
{
textKeyChain[i] = encryptionOperation(textKeyChain[i], encryptedTextKeyChain[i]);
2022-03-20 18:12:25 +01:00
}
//FIX (CLEAN) returningText
for (int i = 0; i <= textKeyChainLength; i++)
{
returningText[i] = '\0';
}
2022-03-20 18:12:25 +01:00
//LOAD returningText
2022-04-27 18:16:37 +02:00
for (int i = 0; i < textKeyChainLength; i++)
2022-03-20 18:12:25 +01:00
{
returningText[i] = textKeyChain[i];
2022-03-20 18:12:25 +01:00
}
//GET FINISH TIME
gettimeofday(&finishTime, NULL);
2022-05-06 17:43:00 +02:00
//LOAD output
outputFlags output =
{
returningText, //DECRYPTED TEXT
key, //USED KEY
countUnusedKeySize(returningText, key), // NUMBER OF UNUSED CHARS IN KEY
countRepeatedKeySize(returningText, key), //NUMBER OF REPEATED CHARS IN KEY
compareTimeMicro(startTime, finishTime), // ELAPSED TIME
SUCCESS //EXIT CODE
2022-05-06 17:43:00 +02:00
};
//DEALLOCATION
free(textKeyChain);
free(encryptedTextKeyChain);
free(textBuffer);
2022-05-06 17:43:00 +02:00
return output;
2022-03-20 18:12:25 +01:00
}