WHY2/src/core/lib/encrypter.c

164 lines
4.2 KiB
C
Raw Normal View History

/*
This is part of WHY2
Copyright (C) 2022 Václav Šmejkal
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-05-01 16:23:07 +02:00
#include <why2/encrypter.h>
2022-03-20 18:12:25 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
2022-04-04 18:51:25 +02: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 encryptText(char *text, char *keyNew)
2022-03-20 18:12:25 +01:00
{
//CHECK VARIABLE
2022-11-03 17:17:15 +01:00
unsigned char checkExitCode;
//TIME VARIABLES
struct timeval startTime;
struct timeval finishTime;
gettimeofday(&startTime, NULL);
2022-04-04 18:51:25 +02:00
//CHECK FOR ACTIVE VERSION
if ((checkExitCode = checkVersion()) != SUCCESS)
{
return noOutput(checkExitCode);
}
2022-04-04 19:09:51 +02:00
//CHECK FOR INVALID text
if ((checkExitCode = checkText(text)) != SUCCESS)
{
return noOutput(checkExitCode);
}
2022-03-20 18:12:25 +01:00
//VARIABLES
char *key = malloc(getKeyLength() + 1);
2022-03-20 18:12:25 +01:00
char *returningText;
char *textBuffer = malloc(1);
int *textKeyChain = malloc(sizeof(int) * strlen(text));
int numberBuffer;
FILE *fileBuffer;
//TRY TO MAKE RANDOM GENERATION REALLY "RANDOM"
fileBuffer = fopen("/dev/urandom", "r");
(void) (fread(&numberBuffer, sizeof(numberBuffer), 1, fileBuffer) + 1); //TODO: Try to create some function for processing exit value
srand(numberBuffer);
2022-11-17 14:10:25 +01:00
numberBuffer = abs(numberBuffer); //MAKE numberBuffer POSITIVE
2022-03-20 18:12:25 +01:00
if (keyNew != NULL)
{
//CHECK FOR INVALID key
if ((checkExitCode = checkKey(keyNew)) != SUCCESS)
{
return noOutput(checkExitCode);
}
2022-03-20 18:12:25 +01:00
strcpy(key, keyNew);
2022-05-03 18:37:28 +02:00
//REDEFINE keyLength
setKeyLength(strlen(key));
2022-04-26 19:04:43 +02:00
2022-03-20 18:12:25 +01:00
goto skipKey;
}
//LOAD KEY
for (int i = 0; i < (int) getKeyLength(); i++)
2022-03-20 18:12:25 +01:00
{
//SET numberBuffer TO RANDOM NUMBER BETWEEN 0 AND 52
numberBuffer = (rand() % 52) + 1;
2022-03-20 18:12:25 +01:00
//GET CHAR FROM numberBuffer
if (numberBuffer > 26)
{
numberBuffer += 70;
2022-06-10 19:48:45 +02:00
}
else
2022-03-20 18:12:25 +01:00
{
numberBuffer += 64;
}
key[i] = (char) numberBuffer;
}
key[getKeyLength()] = '\0';
2022-05-25 17:58:19 +02:00
2022-03-20 18:12:25 +01:00
skipKey:
//LOAD textKeyChain
2022-04-27 18:07:26 +02:00
generateTextKeyChain(key, textKeyChain, strlen(text));
2022-03-20 18:12:25 +01:00
//ACTUALLY ENCRYPT TEXT
for (int i = 0; i < (int) strlen(text); i++)
2022-03-20 18:12:25 +01:00
{
textKeyChain[i] = getEncryptionOperation()(textKeyChain[i], (int) text[i]);
2022-03-20 18:12:25 +01:00
}
//COUNT REQUIRED SIZE FOR returningText
for (int i = 0; i < (int) strlen(text); i++)
2022-03-20 18:12:25 +01:00
{
2022-04-23 18:15:36 +02:00
numberBuffer += countIntLength(textKeyChain[i]);
2022-03-20 18:12:25 +01:00
}
//ALLOCATE returningText (WITH THE SEPARATORS)
returningText = malloc(numberBuffer + strlen(text) - 1);
2022-04-23 18:16:22 +02:00
strcpy(returningText, "");
2022-03-20 18:12:25 +01:00
//LOAD returningText
for (int i = 0; i < (int) strlen(text); i++)
2022-03-20 18:12:25 +01:00
{
numberBuffer = sizeof(int) * countIntLength(textKeyChain[i]);
2022-12-09 16:16:47 +01:00
textBuffer = realloc(textBuffer, numberBuffer);
2022-03-20 18:12:25 +01:00
sprintf(textBuffer, "%d", textKeyChain[i]);
strcat(returningText, textBuffer);
if (i != (int) strlen(text) - 1)
2022-03-20 18:12:25 +01:00
{
textBuffer = realloc(textBuffer, 2);
2022-06-18 18:30:06 +02:00
sprintf(textBuffer, "%c", getEncryptionSeparator());
strcat(returningText, textBuffer);
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, //ENCRYPTED TEXT
key, //GENERATED/USED KEY
countUnusedKeySize(text, key), // NUMBER OF UNUSED CHARS IN KEY
countRepeatedKeySize(text, 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(textBuffer);
fclose(fileBuffer);
2022-05-06 17:43:00 +02:00
return output;
2022-04-07 17:26:41 +02:00
}