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-07-10 18:13:34 +02:00
|
|
|
outputFlags decryptText(char *text, char *keyNew)
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
2022-06-12 17:06:44 +02:00
|
|
|
//CHECK VARIABLE
|
|
|
|
int checkExitCode;
|
|
|
|
|
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-07-10 18:31:26 +02:00
|
|
|
if ((checkExitCode = checkVersion()) != SUCCESS)
|
2022-06-12 17:06:44 +02:00
|
|
|
{
|
|
|
|
return noOutput(checkExitCode);
|
|
|
|
}
|
2022-05-31 18:44:25 +02:00
|
|
|
|
2022-05-29 16:49:18 +02:00
|
|
|
//CHECK FOR INVALID text
|
2022-07-10 18:31:26 +02:00
|
|
|
if ((checkExitCode = checkText(text)) != SUCCESS)
|
2022-06-12 17:06:44 +02:00
|
|
|
{
|
|
|
|
return noOutput(checkExitCode);
|
|
|
|
}
|
2022-05-29 16:49:18 +02:00
|
|
|
|
2022-03-20 18:12:25 +01:00
|
|
|
//CHECK FOR INVALID key
|
2022-07-10 18:31:26 +02:00
|
|
|
if ((checkExitCode = checkKey(keyNew)) != SUCCESS)
|
2022-06-12 17:06:44 +02:00
|
|
|
{
|
|
|
|
return noOutput(checkExitCode);
|
|
|
|
}
|
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-06-13 18:13:27 +02:00
|
|
|
char *key = malloc(strlen(keyNew) + 1);
|
2022-07-11 17:49:08 +02:00
|
|
|
int *encryptedTextKeyChain;
|
2022-05-26 19:35:22 +02:00
|
|
|
|
|
|
|
//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-06-19 17:19:03 +02:00
|
|
|
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)
|
2022-06-13 18:13:27 +02:00
|
|
|
returningText = malloc(numberBuffer + 1);
|
2022-05-24 18:45:44 +02:00
|
|
|
textKeyChain = malloc(sizeof(int) * numberBuffer);
|
2022-07-11 17:49:08 +02:00
|
|
|
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
|
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
|
2022-06-19 17:19:03 +02:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2022-07-15 19:32:57 +02:00
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
textBuffer = realloc(textBuffer, numberBuffer + 1);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
textBuffer = malloc(numberBuffer + 1);
|
|
|
|
}
|
2022-03-20 18:12:25 +01:00
|
|
|
|
2022-07-19 17:18:00 +02: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
|
2022-06-19 17:19:03 +02:00
|
|
|
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
|
|
|
{
|
2022-10-16 19:46:56 +02:00
|
|
|
textKeyChain[i] = encryptionOperation(textKeyChain[i], encryptedTextKeyChain[i]);
|
2022-03-20 18:12:25 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 17:22:12 +02:00
|
|
|
//FIX (CLEAN) returningText
|
|
|
|
for (int i = 0; i <= textKeyChainLength; i++)
|
2022-05-29 16:44:14 +02:00
|
|
|
{
|
2022-07-19 17:22:12 +02:00
|
|
|
returningText[i] = '\0';
|
2022-05-29 16:44:14 +02:00
|
|
|
}
|
|
|
|
|
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
|
2022-08-24 14:56:21 +02:00
|
|
|
countRepeatedKeySize(returningText, key), //NUMBER OF REPEATED CHARS IN KEY
|
2022-06-12 16:23:26 +02:00
|
|
|
compareTimeMicro(startTime, finishTime), // ELAPSED TIME
|
|
|
|
SUCCESS //EXIT CODE
|
2022-05-06 17:43:00 +02:00
|
|
|
};
|
|
|
|
|
2022-05-11 17:41:45 +02:00
|
|
|
//DEALLOCATION
|
|
|
|
free(textKeyChain);
|
2022-07-11 17:49:08 +02:00
|
|
|
free(encryptedTextKeyChain);
|
2022-07-15 19:32:57 +02:00
|
|
|
free(textBuffer);
|
2022-05-11 17:41:45 +02:00
|
|
|
|
2022-05-06 17:43:00 +02:00
|
|
|
return output;
|
2022-03-20 18:12:25 +01:00
|
|
|
}
|