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 <math.h>
|
2022-05-30 19:09:47 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.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-05-06 17:43:00 +02:00
|
|
|
outputFlags encryptText(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-04-04 18:51:25 +02:00
|
|
|
//CHECK FOR ACTIVE VERSION
|
2022-06-08 19:27:55 +02:00
|
|
|
checkVersion(flags);
|
2022-04-04 19:09:51 +02:00
|
|
|
|
2022-05-31 18:45:24 +02:00
|
|
|
//CHECK FOR INVALID text
|
|
|
|
checkText(text, flags);
|
|
|
|
|
2022-05-29 16:49:18 +02:00
|
|
|
if (strcmp(text, "") == 0)
|
|
|
|
{
|
|
|
|
if (!flags.noOutput) fprintf(stderr, "No text to encrypt!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-03-20 18:12:25 +01:00
|
|
|
//VARIABLES
|
2022-05-26 17:45:10 +02:00
|
|
|
char *key = malloc(getKeyLength());
|
2022-03-20 18:12:25 +01:00
|
|
|
char *returningText;
|
|
|
|
char *textBuffer;
|
2022-05-24 18:45:44 +02:00
|
|
|
int *textKeyChain = malloc(sizeof(int) * strlen(text));
|
2022-05-21 16:26:24 +02:00
|
|
|
int numberBuffer;
|
|
|
|
FILE *fileBuffer;
|
|
|
|
|
|
|
|
//TRY TO MAKE RANDOM GENERATION REALLY "RANDOM"
|
|
|
|
fileBuffer = fopen("/dev/urandom", "r");
|
|
|
|
fread(&numberBuffer, 4, 1, fileBuffer);
|
|
|
|
srand(numberBuffer);
|
2022-05-24 18:21:33 +02:00
|
|
|
fclose(fileBuffer);
|
2022-03-20 18:12:25 +01:00
|
|
|
|
|
|
|
if (keyNew != NULL)
|
|
|
|
{
|
2022-05-26 19:11:52 +02:00
|
|
|
checkKey(keyNew, flags); //CHECK FOR INVALID key
|
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
|
2022-05-03 18:37:28 +02:00
|
|
|
for (int i = 0; i < getKeyLength(); i++)
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
|
|
|
//SET numberBuffer TO RANDOM NUMBER BETWEEN 0 AND 52
|
|
|
|
numberBuffer = (rand() % 52) + 1;
|
2022-05-21 15:46:19 +02:00
|
|
|
|
2022-03-20 18:12:25 +01:00
|
|
|
//GET CHAR FROM numberBuffer
|
|
|
|
if (numberBuffer > 26)
|
|
|
|
{
|
|
|
|
numberBuffer += 70;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
numberBuffer += 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
key[i] = (char) numberBuffer;
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:58:19 +02:00
|
|
|
key[getKeyLength()] = '\0'; //TODO: CHECK IF THIS CAN'T CAUSE SOME LITTLE TROUBLES (SHOULDN'T)
|
|
|
|
|
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 < strlen(text); i++)
|
|
|
|
{
|
|
|
|
textKeyChain[i] -= (int) text[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
//COUNT REQUIRED SIZE FOR returningText
|
2022-04-27 18:07:26 +02:00
|
|
|
for (int i = 0; i < 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-04-27 18:07:26 +02:00
|
|
|
for (int i = 0; i < 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
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
textBuffer = realloc(textBuffer, numberBuffer);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
textBuffer = malloc(numberBuffer);
|
|
|
|
}
|
2022-03-20 18:12:25 +01:00
|
|
|
|
|
|
|
sprintf(textBuffer, "%d", textKeyChain[i]);
|
|
|
|
|
|
|
|
strcat(returningText, textBuffer);
|
|
|
|
|
2022-04-27 18:07:26 +02:00
|
|
|
if (i != strlen(text) - 1)
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
|
|
|
strcat(returningText, ENCRYPTION_SEPARATOR_STRING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-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
|
|
|
}
|