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>
|
2022-05-30 19:09:47 +02:00
|
|
|
#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
|
|
|
|
2022-05-26 19:35:22 +02:00
|
|
|
outputFlags decryptText(char *text, char *keyNew, inputFlags flags)
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
2022-05-30 19:09:47 +02:00
|
|
|
//TIME VARIABLES
|
|
|
|
struct timeval startTime;
|
|
|
|
struct timeval finishTime;
|
|
|
|
gettimeofday(&startTime, NULL);
|
|
|
|
|
2022-05-31 18:44:25 +02:00
|
|
|
//CHECK FOR ACTIVE VERSION
|
2022-06-08 19:26:24 +02:00
|
|
|
if (!flags.noCheck) checkVersion(flags);
|
2022-05-31 18:44:25 +02:00
|
|
|
|
2022-05-29 16:49:18 +02:00
|
|
|
//CHECK FOR INVALID text
|
|
|
|
checkText(text, flags);
|
|
|
|
|
2022-03-20 18:12:25 +01:00
|
|
|
//CHECK FOR INVALID key
|
2022-05-26 19:35:22 +02:00
|
|
|
checkKey(keyNew, flags);
|
2022-04-26 19:04:43 +02:00
|
|
|
|
2022-05-03 18:37:28 +02:00
|
|
|
//REDEFINE keyLength
|
2022-05-26 19:35:22 +02:00
|
|
|
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;
|
2022-05-26 19:35:22 +02:00
|
|
|
char *key = malloc(strlen(keyNew));
|
|
|
|
|
|
|
|
//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
|
2022-03-20 18:12:25 +01:00
|
|
|
for (int i = 0; i < strlen(text); i++)
|
|
|
|
{
|
|
|
|
if (text[i] == ENCRYPTION_SEPARATOR) numberBuffer++;
|
|
|
|
}
|
|
|
|
|
2022-04-27 18:16:37 +02:00
|
|
|
//SET LENGTH (numberBuffer)
|
2022-05-26 17:44:04 +02:00
|
|
|
returningText = malloc(numberBuffer);
|
2022-05-24 18:45:44 +02:00
|
|
|
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
|
2022-05-29 16:44:14 +02:00
|
|
|
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 < strlen(text); j++)
|
|
|
|
{
|
|
|
|
if (text[j] == ENCRYPTION_SEPARATOR) break;
|
|
|
|
|
|
|
|
numberBuffer++;
|
|
|
|
}
|
|
|
|
|
2022-05-24 19:01:31 +02:00
|
|
|
textBuffer = malloc(numberBuffer);
|
2022-03-20 18:12:25 +01:00
|
|
|
|
|
|
|
//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
|
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];
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:44:14 +02:00
|
|
|
//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
|
|
|
{
|
2022-05-29 16:44:14 +02:00
|
|
|
returningText[i] = textKeyChain[i];
|
2022-03-20 18:12:25 +01:00
|
|
|
}
|
|
|
|
|
2022-05-30 19:09:47 +02:00
|
|
|
//GET FINISH TIME
|
|
|
|
gettimeofday(&finishTime, NULL);
|
|
|
|
|
2022-05-06 17:43:00 +02:00
|
|
|
//LOAD output
|
|
|
|
outputFlags output =
|
|
|
|
{
|
|
|
|
returningText, //DECRYPTED TEXT
|
2022-05-30 18:01:21 +02:00
|
|
|
key, //USED KEY
|
2022-05-30 19:09:47 +02:00
|
|
|
countUnusedKeySize(returningText, key), // NUMBER OF UNUSED CHARS IN KEY
|
|
|
|
compareTimeMicro(startTime, finishTime) // ELAPSED TIME
|
2022-05-06 17:43:00 +02:00
|
|
|
};
|
|
|
|
|
2022-05-11 17:41:45 +02:00
|
|
|
//DEALLOCATION
|
|
|
|
free(textKeyChain);
|
|
|
|
|
2022-05-06 17:43:00 +02:00
|
|
|
return output;
|
2022-03-20 18:12:25 +01:00
|
|
|
}
|