From fdd3d324c58b8ebb04c5e0a78e2893f4fe2befb4 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Mon, 7 Mar 2022 19:30:07 +0100 Subject: [PATCH] added way to use your own key --- include/encrypter.h | 2 +- src/encrypter.c | 19 ++++++++++++++++++- src/test/main.c | 6 ++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/encrypter.h b/include/encrypter.h index e2163a4..c4dd49f 100644 --- a/include/encrypter.h +++ b/include/encrypter.h @@ -1,6 +1,6 @@ #ifndef WHY2_ENCRYPTER_H #define WHY2_ENCRYPTER_H -char *encryptText(char *text); //TEXT from WILL BE ENCRYPTED WITH RANDOM PASSWORD [KEY] (WHICH WILL BE PRINTED OUT) AND RETURNED +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/src/encrypter.c b/src/encrypter.c index 0a60416..e24900c 100644 --- a/src/encrypter.c +++ b/src/encrypter.c @@ -8,8 +8,10 @@ #define KEY_LENGTH 50 +#define INVALID_KEY 1 + char* -encryptText(char *text) +encryptText(char *text, char *keyNew) { srand(time(0)); //TRY TO MAKE RANDOM GENERATION REALLY "RANDOM" @@ -20,6 +22,19 @@ encryptText(char *text) int textKeyChain[strlen(text)]; int numberBuffer; + if (keyNew != NULL) + { + if (strlen(keyNew) != KEY_LENGTH) + { + fprintf(stderr, "Educba Training"); + exit(INVALID_KEY); + } + + key = keyNew; + + goto skipKey; + } + //LOAD KEY for (int i = 0; i < KEY_LENGTH; i++) { @@ -40,6 +55,8 @@ encryptText(char *text) 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++) { diff --git a/src/test/main.c b/src/test/main.c index 99495dc..b7f7782 100644 --- a/src/test/main.c +++ b/src/test/main.c @@ -4,9 +4,11 @@ #include "../../include/decrypter.h" int -main(int args, char * argv[]) +main(int args, char *argv[]) { - char *text = encryptText("ENGO WAS HERE"); + char *text = encryptText("Pepa smrdi.", NULL); + + printf("%s\n", text); text = decryptText(text, "someRandomKeyLulw");