2022-12-04 18:54:57 +01:00
|
|
|
/*
|
|
|
|
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>
|
2022-05-30 19:09:47 +02:00
|
|
|
#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
|
|
|
|
2022-07-10 18:13:34 +02:00
|
|
|
outputFlags encryptText(char *text, char *keyNew)
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
2022-06-12 17:04:47 +02:00
|
|
|
//CHECK VARIABLE
|
2022-11-03 17:17:15 +01:00
|
|
|
unsigned char checkExitCode;
|
2022-06-12 17:04:47 +02:00
|
|
|
|
2022-05-30 19:09:47 +02:00
|
|
|
//TIME VARIABLES
|
|
|
|
struct timeval startTime;
|
|
|
|
struct timeval finishTime;
|
|
|
|
gettimeofday(&startTime, NULL);
|
|
|
|
|
2022-04-04 18:51:25 +02:00
|
|
|
//CHECK FOR ACTIVE VERSION
|
2022-07-10 18:31:26 +02:00
|
|
|
if ((checkExitCode = checkVersion()) != SUCCESS)
|
2022-06-12 17:04:47 +02:00
|
|
|
{
|
|
|
|
return noOutput(checkExitCode);
|
|
|
|
}
|
2022-04-04 19:09:51 +02:00
|
|
|
|
2022-05-31 18:45:24 +02:00
|
|
|
//CHECK FOR INVALID text
|
2022-07-10 18:31:26 +02:00
|
|
|
if ((checkExitCode = checkText(text)) != SUCCESS)
|
2022-06-12 17:04:47 +02:00
|
|
|
{
|
|
|
|
return noOutput(checkExitCode);
|
|
|
|
}
|
2022-05-31 18:45:24 +02:00
|
|
|
|
2022-03-20 18:12:25 +01:00
|
|
|
//VARIABLES
|
2022-06-13 18:13:27 +02:00
|
|
|
char *key = malloc(getKeyLength() + 1);
|
2022-03-20 18:12:25 +01:00
|
|
|
char *returningText;
|
2022-12-04 18:59:27 +01:00
|
|
|
char *textBuffer = malloc(1);
|
2022-05-24 18:45:44 +02:00
|
|
|
int *textKeyChain = malloc(sizeof(int) * strlen(text));
|
2022-12-09 18:03:30 +01:00
|
|
|
int numberBuffer = 0;
|
2022-03-20 18:12:25 +01:00
|
|
|
|
|
|
|
if (keyNew != NULL)
|
|
|
|
{
|
2022-06-12 17:04:47 +02:00
|
|
|
//CHECK FOR INVALID key
|
2022-07-10 18:31:26 +02:00
|
|
|
if ((checkExitCode = checkKey(keyNew)) != SUCCESS)
|
2022-06-12 17:04:47 +02:00
|
|
|
{
|
|
|
|
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-12-09 16:59:12 +01:00
|
|
|
} else //LOAD KEY
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
2022-12-09 16:59:12 +01:00
|
|
|
generateKey(key, getKeyLength());
|
2022-03-20 18:12:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
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-10-17 16:47:05 +02:00
|
|
|
textKeyChain[i] = getEncryptionOperation()(textKeyChain[i], (int) text[i]);
|
2022-03-20 18:12:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//COUNT REQUIRED SIZE FOR returningText
|
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-04-23 18:15:36 +02:00
|
|
|
numberBuffer += countIntLength(textKeyChain[i]);
|
2022-03-20 18:12:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//ALLOCATE returningText (WITH THE SEPARATORS)
|
2022-05-24 18:56:32 +02:00
|
|
|
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
|
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-05-24 18:45:44 +02:00
|
|
|
numberBuffer = sizeof(int) * countIntLength(textKeyChain[i]);
|
2022-05-22 15:26:17 +02:00
|
|
|
|
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);
|
|
|
|
|
2022-06-19 17:19:03 +02:00
|
|
|
if (i != (int) strlen(text) - 1)
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
2022-06-18 18:17:54 +02:00
|
|
|
textBuffer = realloc(textBuffer, 2);
|
2022-06-18 18:30:06 +02:00
|
|
|
sprintf(textBuffer, "%c", getEncryptionSeparator());
|
2022-06-18 18:17:54 +02:00
|
|
|
|
|
|
|
strcat(returningText, textBuffer);
|
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, //ENCRYPTED TEXT
|
2022-05-30 18:01:21 +02:00
|
|
|
key, //GENERATED/USED KEY
|
2022-05-30 19:09:47 +02:00
|
|
|
countUnusedKeySize(text, key), // NUMBER OF UNUSED CHARS IN KEY
|
2022-08-24 15:45:25 +02:00
|
|
|
countRepeatedKeySize(text, 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-05-22 15:26:17 +02:00
|
|
|
free(textBuffer);
|
2022-05-11 17:41:45 +02:00
|
|
|
|
2022-05-06 17:43:00 +02:00
|
|
|
return output;
|
2022-04-07 17:26:41 +02:00
|
|
|
}
|