diff --git a/ASYD_Cryptograhpy/SW03-DHKE/main.c b/ASYD_Cryptograhpy/SW03-DHKE/main.c index 2c357df..7bafdee 100644 --- a/ASYD_Cryptograhpy/SW03-DHKE/main.c +++ b/ASYD_Cryptograhpy/SW03-DHKE/main.c @@ -8,7 +8,7 @@ * \author Basil Estermann, basil.estermann@stud.hslu.ch * Simon Frei, simon.frei@stud.hslu.ch * Jonas Arnold, jonas.arnold@stud.hslu.ch - * \date 09.03.2023 + * \date 10.03.2023 * *-------------------------------------------------------------------- */ @@ -21,23 +21,21 @@ #include "DHKE.h" -#define KEY_LENGTH 32 - -/* filenames as of this code: - */ +#define KEY_LENGTH 64 void main(void) { /* Initialize variables */ - // P has to be a prime number! + // P has to be a prime number! Large prime number (64bit): 3203431780337000 t_encryptionArithmetic P; encryptionArithmetic_Init(&P, KEY_LENGTH); - if (encryptionArithmetic_stringToHex("99497", P.number, KEY_LENGTH) == false) { + if (encryptionArithmetic_stringToHex("3203431780337000", P.number, KEY_LENGTH) == false) { printf("ERROR: Creating P."); return 1000000; } - + + // alpha not required for session key generation //t_encryptionArithmetic alpha; //encryptionArithmetic_Init(&alpha, KEY_LENGTH); //if (encryptionArithmetic_stringToHex("13", alpha.number, KEY_LENGTH) == false) { @@ -47,14 +45,14 @@ void main(void) { t_encryptionArithmetic pub; encryptionArithmetic_Init(&pub, KEY_LENGTH); - if (encryptionArithmetic_stringToHex("1100", pub.number, KEY_LENGTH) == false) { + if (encryptionArithmetic_stringToHex("1370122177720875", pub.number, KEY_LENGTH) == false) { printf("ERROR: Creating pub."); return 1000002; } t_encryptionArithmetic priv; encryptionArithmetic_Init(&priv, KEY_LENGTH); - if (encryptionArithmetic_stringToHex("2100", priv.number, KEY_LENGTH) == false) { + if (encryptionArithmetic_stringToHex("9778729279583412", priv.number, KEY_LENGTH) == false) { printf("ERROR: Creating priv."); return 1000003; } @@ -72,132 +70,4 @@ void main(void) { return 0; -} - -//void encrypt(uint32_t key[4], uint8_t numberOfCycles) { -// -// /* duplicate code that hasn't been moved to a separate function for readability reasons */ -// /* -------------------------------------------------------------------- */ -// -// /* variables */ -// uint32_t amountOfCharsInFile = 0; -// /* https://www.programiz.com/c-programming/c-file-input-output */ -// FILE* fileHandle = fopen("plaintext.txt", "r"); -// if (fileHandle == NULL) { -// return; -// } -// -// /* get total number of characters in file to encrypt */ -// while (getc(fileHandle) != EOF) { -// amountOfCharsInFile++; -// } -// -// /* allocate enough memory for the whole file to be read */ -// char* inputText = (char*)calloc(amountOfCharsInFile + 8, sizeof(char)); /* add 8 for extra padding in case the file contains some sort of rest in regards to amountOfCharsInFile % 8 */ -// if (inputText == NULL) { -// return; -// } -// -// /* close and reopen file to start at the beginning of the file again */ -// fclose(fileHandle); -// fileHandle = fopen("plaintext.txt", "r"); -// if (fileHandle == NULL) { -// return; -// } -// -// /* read the whole file into memory */ -// for (uint32_t i = 0; i < amountOfCharsInFile; i++) { -// inputText[i] = getc(fileHandle); -// } -// fclose(fileHandle); -// /* -------------------------------------------------------------------- */ -// -// /* loop through the whole array to encrypt the whole message -// This happens two blocks of 32bit (= 8 characters) at a time -// Hence the increase of "i" by 8 -// */ -// for (uint32_t i = 0; i < amountOfCharsInFile; i += 8) { /* + 8 to compensate for the requested element size of the encipher function (32 bit) */ -// /* dereferencing and forwarding the address of the key ensures, -// * that we don't ready from a random address in memory. Rookie mistakes happen :) */ -// encipher_CBC(numberOfCycles, &(inputText[i]), &(*key)); -// } -// -// /* generate new file where encryption is stored */ -// fileHandle = fopen("encrypted.cip", "w"); -// if (fileHandle == NULL) { -// return; -// } -// /* fill new file with content */ -// /* to get the extra character padding we might unintentionally drop -// * we need to read up to a multiple of 8 (32 bit) -// */ -// for (uint32_t i = 0; i < (amountOfCharsInFile + (8-(amountOfCharsInFile%8))); i++) { -// putc(inputText[i], fileHandle); -// } -// fclose(fileHandle); -// free(inputText); -//} -// -//void decrypt(uint32_t key[4], uint8_t numberOfCycles) { -// -// /* duplicate code that hasn't been moved to a separate function for readability reasons */ -// /* -------------------------------------------------------------------- */ -// -// /* variables */ -// uint32_t amountOfCharsInFile = 0; -// -// /* https://www.programiz.com/c-programming/c-file-input-output */ -// FILE* fileHandle = fopen("encrypted.cip", "r"); -// if (fileHandle == NULL) { -// return; -// } -// -// /* get total number of characters in file to decrypt */ -// while (getc(fileHandle) != EOF) { -// amountOfCharsInFile++; -// } -// -// /* allocate memory for the whole file to be read */ -// char* inputText = (char*)calloc(amountOfCharsInFile + 10, sizeof(char)); /* add 10 for extra padding */ -// if (inputText == NULL) { -// return; -// } -// -// /* close and reopen file to start at the beginning of the file again */ -// fclose(fileHandle); -// fileHandle = fopen("encrypted.cip", "r"); -// if (fileHandle == NULL) { -// return; -// } -// -// /* read the whole file into memory */ -// for (uint32_t i = 0; i < amountOfCharsInFile; i++) { -// inputText[i] = getc(fileHandle); -// } -// fclose(fileHandle); -// -// /* -------------------------------------------------------------------- */ -// -// /* loop through the whole array to encrypt the whole message -// This happens two blocks of 32bit (= 4 characters) at a time -// Hence the increase of "i" by 8 -// */ -// -// for (uint32_t i = 0; i < amountOfCharsInFile; i += 8) { /* i += 8*/ -// /* dereferencing and forwarding the address of the key ensures, -// * that we don't ready from a random address in memory. Rookie mistakes happen :) */ -// decipher_CBC(numberOfCycles, &(inputText[i]), &(*key)); -// } -// -// /* generate new file where the decryption is stored */ -// fileHandle = fopen("decrypted.txt", "w"); -// if (fileHandle == NULL) { -// return; -// } -// /* fill new file with content */ -// for (uint32_t i = 0; i < amountOfCharsInFile; i++) { -// putc(inputText[i], fileHandle); -// } -// fclose(fileHandle); -// free(inputText); -//} \ No newline at end of file +} \ No newline at end of file