WHY2/src/lib/decrypter.c

134 lines
3.0 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
2022-07-10 18:18:36 +02:00
if ((checkExitCode = checkVersion(getFlags())) != SUCCESS)
{
return noOutput(checkExitCode);
}
//CHECK FOR INVALID text
2022-07-10 18:18:36 +02:00
if ((checkExitCode = checkText(text, getFlags())) != SUCCESS)
{
return noOutput(checkExitCode);
}
2022-03-20 18:12:25 +01:00
//CHECK FOR INVALID key
2022-07-10 18:18:36 +02:00
if ((checkExitCode = checkKey(keyNew, getFlags())) != 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);
//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);
int encryptedTextKeyChain[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++;
}
textBuffer = malloc(numberBuffer + 1);
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;
free(textBuffer);
}
//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] -= encryptedTextKeyChain[i];
}
//FIX returningText
strcpy(returningText, "");
for (int i = 0; i < textKeyChainLength; i++)
{
strcat(returningText, " ");
}
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
compareTimeMicro(startTime, finishTime), // ELAPSED TIME
SUCCESS //EXIT CODE
2022-05-06 17:43:00 +02:00
};
//DEALLOCATION
free(textKeyChain);
2022-05-06 17:43:00 +02:00
return output;
2022-03-20 18:12:25 +01:00
}