/** *--------------------------------------------------------------------\n * HSLU T&A Hochschule Luzern Technik+Architektur \n *--------------------------------------------------------------------\n * * \brief ASYD assignment crypto 03 * \file * \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 * *-------------------------------------------------------------------- */ #include #include #include #include #include "encryptionArithmetic.h" #include "DHKE.h" #define KEY_LENGTH 32 /* filenames as of this code: */ void main(void) { /* Initialize variables */ t_encryptionArithmetic P; encryptionArithmetic_Init(&P, KEY_LENGTH); if (encryptionArithmetic_stringToHex("19", P.number, KEY_LENGTH) == false) { printf("ERROR: Creating P."); return 1000000; } //t_encryptionArithmetic alpha; //encryptionArithmetic_Init(&alpha, KEY_LENGTH); //if (encryptionArithmetic_stringToHex("13", alpha.number, KEY_LENGTH) == false) { // printf("ERROR: Creating alpha."); // return 1000001; //} t_encryptionArithmetic pub; encryptionArithmetic_Init(&pub, KEY_LENGTH); if (encryptionArithmetic_stringToHex("11", pub.number, KEY_LENGTH) == false) { printf("ERROR: Creating pub."); return 1000002; } t_encryptionArithmetic priv; encryptionArithmetic_Init(&priv, KEY_LENGTH); if (encryptionArithmetic_stringToHex("21", priv.number, KEY_LENGTH) == false) { printf("ERROR: Creating priv."); return 1000003; } t_encryptionArithmetic session_key; encryptionArithmetic_Init(&session_key, KEY_LENGTH); /* Create session key */ squareAndMultiply(&pub, &priv, &P, &session_key, KEY_LENGTH); /* Write session key */ printf("%i",*session_key.number); 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); //}