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 <time.h>
|
|
|
|
#include <math.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
|
|
|
|
|
|
|
char*
|
|
|
|
encryptText(char *text, char *keyNew)
|
|
|
|
{
|
2022-04-04 18:51:25 +02:00
|
|
|
//CHECK FOR ACTIVE VERSION
|
2022-04-22 18:40:03 +02:00
|
|
|
checkVersion();
|
2022-04-04 19:09:51 +02:00
|
|
|
|
2022-03-20 18:12:25 +01:00
|
|
|
srand(time(0)); //TRY TO MAKE RANDOM GENERATION REALLY "RANDOM"
|
|
|
|
|
|
|
|
//VARIABLES
|
2022-05-03 18:37:28 +02:00
|
|
|
char *key = malloc(getKeyLength());
|
2022-03-20 18:12:25 +01:00
|
|
|
char *returningText;
|
|
|
|
char *textBuffer;
|
2022-04-27 18:07:26 +02:00
|
|
|
int *textKeyChain = malloc(strlen(text) * sizeof(int));
|
|
|
|
int numberBuffer = 0;
|
2022-03-20 18:12:25 +01:00
|
|
|
|
|
|
|
if (keyNew != NULL)
|
|
|
|
{
|
2022-05-03 18:37:28 +02:00
|
|
|
if (strlen(keyNew) < getKeyLength())
|
2022-03-20 18:12:25 +01:00
|
|
|
{
|
2022-05-03 18:37:28 +02:00
|
|
|
fprintf(stderr, "Key must be at least %d characters long!\n", getKeyLength());
|
2022-03-20 18:12:25 +01:00
|
|
|
exit(INVALID_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
//GET CHAR FROM numberBuffer
|
|
|
|
if (numberBuffer > 26)
|
|
|
|
{
|
|
|
|
numberBuffer += 70;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
numberBuffer += 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
key[i] = (char) numberBuffer;
|
|
|
|
}
|
|
|
|
|
2022-05-04 18:44:54 +02:00
|
|
|
if (!noOutput) printf("Your key is: %s\n!!! SAVE IT SOMEWHERE !!!\n\n", key);
|
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-04-27 18:07:26 +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-04-23 18:15:36 +02:00
|
|
|
textBuffer = malloc(countIntLength(textKeyChain[i]));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(textBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
free(key);
|
2022-04-23 17:47:15 +02:00
|
|
|
|
2022-03-20 18:12:25 +01:00
|
|
|
return returningText;
|
2022-04-07 17:26:41 +02:00
|
|
|
}
|