diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1f9408c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Václav Šmejkal + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..09cd2d2 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +all: main + +# Main file +files = src/test/main.c + +# Source files +files += src/*.c + +# Header files +files += include/*.h + +main: + @echo Compiling... + cc $(files) -lm -o out/why2 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c3a3d0 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# WHY2 Encryption System + +*Yeah.* + +This project is made 'like a library', so compiling is useless... :) + +### Table of contents + + - [Using in Your projects](#using-in-your-projects) + - [Example of code](#example-of-code) + - [Example programs](#example-programs) + +## Using in Your projects + +To **encrypt** text, use function `encryptText()` from file `include/encrypter.h`. + +To **decrypt** text, use function `decryptText()` from file `include/decrypter.h`. + +Jump to [examples](#examples) if you're not sure, how to use. + +## Example of code + +- Encryption: + ```c + //FIRST VARIANT + char *yourText = encryptText("Put here text, you want encrypt...", "tzXlZGxkhfYOvRthqokDrmGFyDMylgmeIlrJTpVAwuqrLjABXM"); //The second thing is Your **key**. (The key must be 50 characters long!) + + //SECOND VARIANT + char *yourText = encryptText("Put here text, you want encrypt...", NULL); //See? You don't have to use Your key. Program will automatically generate one for you. It will be printed out, so save it somewhere. + ``` + +- Decryption: + ```c + char *yourText = decryptText("158.-83.9388.-14.57.8419.113.-98.10576", "tzXlZGxkhfYOvRthqokDrmGFyDMylgmeIlrJTpVAwuqrLjABXM"); //First parameter is Your encrypted text, the second is key you want to use for decryption it. + ``` + +## Example programs + +Uhm... There aren't any examples (for now)... I will maybe create some... Later... \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..459e85f --- /dev/null +++ b/build.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +make + +if [ "$1" == "debug" ]; then + ./out/why2 +fi \ No newline at end of file diff --git a/include/decrypter.h b/include/decrypter.h new file mode 100644 index 0000000..a56e547 --- /dev/null +++ b/include/decrypter.h @@ -0,0 +1,6 @@ +#ifndef WHY2_DECRYPTER_H +#define WHY2_DECRYPTER_H + +char *decryptText(char *text, char *key); //TEXT from WILL BE DECRYPTED WITH KEY AND RETURNED + +#endif \ No newline at end of file diff --git a/include/encrypter.h b/include/encrypter.h new file mode 100644 index 0000000..c4dd49f --- /dev/null +++ b/include/encrypter.h @@ -0,0 +1,6 @@ +#ifndef WHY2_ENCRYPTER_H +#define WHY2_ENCRYPTER_H + +char *encryptText(char *text, char *keyNew); //TEXT from WILL BE ENCRYPTED WITH RANDOM PASSWORD [KEY] (WHICH WILL BE PRINTED OUT) AND RETURNED + +#endif \ No newline at end of file diff --git a/include/flags.h b/include/flags.h new file mode 100644 index 0000000..7bc540a --- /dev/null +++ b/include/flags.h @@ -0,0 +1,10 @@ +#ifndef WHY2_FLAGS_H +#define WHY2_FLAGS_H + +#define KEY_LENGTH 50 +#define ENCRYPTION_SEPARATOR '.' +#define ENCRYPTION_SEPARATOR_STRING "." + +#define INVALID_KEY 1 + +#endif \ No newline at end of file diff --git a/src/decrypter.c b/src/decrypter.c new file mode 100644 index 0000000..7162ae7 --- /dev/null +++ b/src/decrypter.c @@ -0,0 +1,103 @@ +#include "../include/decrypter.h" + +#include +#include +#include + +#include "../include/flags.h" + +char* +decryptText(char *text, char *key) +{ + //CHECK FOR INVALID key + if (strlen(key) != KEY_LENGTH) + { + fprintf(stderr, "Key must be 50 characters long!\n"); + exit(INVALID_KEY); + } + + //VARIABLES + char *returningText; + int numberBuffer; + char *textBuffer; + + numberBuffer = 1; + + //GET LENGHT OF returningText AND textKeyChain + for (int i = 0; i < strlen(text); i++) + { + if (text[i] == ENCRYPTION_SEPARATOR) numberBuffer++; + } + + //SET LENGTH + returningText = malloc(numberBuffer); + int textKeyChain[numberBuffer]; + int encryptedTextKeyChain[numberBuffer]; + + //LOAD textKeyChain + for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) + { + numberBuffer = i; + + //CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH + while (numberBuffer >= KEY_LENGTH) + { + numberBuffer -= KEY_LENGTH; + } + + //FILL textKeyChain + if ((numberBuffer + 1) % 3 == 0) + { + textKeyChain[i] = key[numberBuffer] * key[numberBuffer + 1]; + } else if ((numberBuffer + 1) % 2 == 0) + { + textKeyChain[i] = key[numberBuffer] - key[numberBuffer + 1]; + } else + { + textKeyChain[i] = key[numberBuffer] + key[numberBuffer + 1]; + } + } + + //LOAD encryptedTextKeyChain + for (int i = 0; i < (sizeof(encryptedTextKeyChain) / sizeof(int)); i++) + { + numberBuffer = 0; + + //GET LENGTH OF EACH CHARACTER + for (int j = 0; j < strlen(text); j++) + { + if (text[j] == ENCRYPTION_SEPARATOR) break; + + numberBuffer++; + } + + textBuffer = malloc(numberBuffer); + + //LOAD textBuffer + for (int j = 0; j < strlen(text); j++) + { + textBuffer[j] = text[j]; + + if (numberBuffer == j) break; + } + + encryptedTextKeyChain[i] = atoi(textBuffer); + + text += numberBuffer + 1; + free(textBuffer); + } + + //DECRYPT TEXT + for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) + { + textKeyChain[i] -= encryptedTextKeyChain[i]; + } + + //LOAD returningText + for (int i = 0; i < sizeof(textKeyChain) / sizeof(int); i++) + { + returningText[i] = (char) textKeyChain[i]; + } + + return returningText; +} diff --git a/src/encrypter.c b/src/encrypter.c new file mode 100644 index 0000000..f2cfd52 --- /dev/null +++ b/src/encrypter.c @@ -0,0 +1,123 @@ +#include "../include/encrypter.h" + +#include +#include +#include +#include +#include + +#include "../include/flags.h" + +char* +encryptText(char *text, char *keyNew) +{ + srand(time(0)); //TRY TO MAKE RANDOM GENERATION REALLY "RANDOM" + + //VARIABLES + char *key = malloc(KEY_LENGTH); + char *returningText; + char *textBuffer; + int textKeyChain[strlen(text)]; + int numberBuffer; + + if (keyNew != NULL) + { + if (strlen(keyNew) != KEY_LENGTH) + { + fprintf(stderr, "Key must be 50 characters long!\n"); + exit(INVALID_KEY); + } + + strcpy(key, keyNew); + + goto skipKey; + } + + //LOAD KEY + for (int i = 0; i < KEY_LENGTH; i++) + { + //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; + } + + printf("Your key is: %s\n!!! SAVE IT SOMEWHERE !!!\n\n", key); + + skipKey: + + //LOAD textKeyChain + for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) + { + numberBuffer = i; + + //CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH + while (numberBuffer >= KEY_LENGTH) + { + numberBuffer -= KEY_LENGTH; + } + + //FILL textKeyChain + if ((numberBuffer + 1) % 3 == 0) + { + textKeyChain[i] = key[numberBuffer] * key[numberBuffer + 1]; + } else if ((numberBuffer + 1) % 2 == 0) + { + textKeyChain[i] = key[numberBuffer] - key[numberBuffer + 1]; + } else + { + textKeyChain[i] = key[numberBuffer] + key[numberBuffer + 1]; + } + } + + //ACTUALLY ENCRYPT TEXT + for (int i = 0; i < strlen(text); i++) + { + textKeyChain[i] -= (int) text[i]; + } + + numberBuffer = 0; + + //COUNT REQUIRED SIZE FOR returningText + for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) + { + numberBuffer += floor(log10(abs(textKeyChain[i]))) + 1; + + //CHECK FOR MINUS + if (textKeyChain[i] > 0) numberBuffer++; + } + + //ALLOCATE returningText (WITH THE SEPARATORS) + returningText = malloc(numberBuffer + (sizeof(textKeyChain) / sizeof(int) - 1)); + + //LOAD returningText + for (int i = 0; i < (sizeof(textKeyChain) / sizeof(int)); i++) + { + textBuffer = malloc(floor(log10(abs(textKeyChain[i])))); + + sprintf(textBuffer, "%d", textKeyChain[i]); + + strcat(returningText, textBuffer); + + if (i != (sizeof(textKeyChain) / sizeof(int) - 1)) + { + strcat(returningText, ENCRYPTION_SEPARATOR_STRING); + } + + free(textBuffer); + } + + //DEALLOCATION + free(key); + + return returningText; +} \ No newline at end of file diff --git a/src/test/main.c b/src/test/main.c new file mode 100644 index 0000000..cfe3ee6 --- /dev/null +++ b/src/test/main.c @@ -0,0 +1,18 @@ +#include +#include + +#include "../../include/encrypter.h" +#include "../../include/decrypter.h" + +int +main(int args, char *argv[]) +{ + char *text = encryptText("Pepa smrdí.", "dsadhagsdhuhasvbdzgavdgasvgzduasvgzdavdhbashudbuas"); + printf("%s\n", text); + + text = decryptText(text, "dsadhagsdhuhasvbdzgavdgasvgzduasvgzdavdhbashudbuas"); + printf("%s\n", text); + + free(text); + return 0; +} \ No newline at end of file