Zoltan Hudak / Mbed OS Enigma_Hello

Dependencies:   Enigma

main.cpp

Committer:
hudakz
Date:
2019-09-24
Revision:
2:ac7a3cea1757
Parent:
1:ad5fb6f2ab3c
Child:
3:83f583d06005

File content as of revision 2:ac7a3cea1757:

#include "mbed.h"
#include "Enigma.h"

Serial      pc(USBTX, USBRX);
DigitalOut  led1(LED1);
uint8_t     plugboard[][2] = { { 4, 183 }, { 72, 247 }, { 108, 192 } }; // Define pairs of plugs connected with cords.
Enigma      enigma(Enigma::ROTOR_IV, Enigma::ROTOR_I, Enigma::ROTOR_III, 126, 247, 14, plugboard, 3);

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int main()
{
    char        message[] = "Hello World!"; // A message (byte array) to encrypt.
    uint8_t*    encrypted = new uint8_t[strlen(message)];
    uint8_t*    decrypted = new uint8_t[strlen(message) + 1];

    // Helper functions for maintenance only:
    //enigma.genRotorWiring("IV", 213); // Generate new wiring for rotor IV.
    //enigma.genReflectorWiring(6);     // Generate new wiring for the reflector.
    while (true) {
        pc.printf("------------------------------\r\n");
        // Print the 'message'.
        pc.printf("%s\r\n\r\n", message);

        // Encrypt the 'message' and print.
        enigma.encrypt(encrypted, (uint8_t*)message, strlen(message));
        for (size_t i = 0; i < strlen(message); i++) {
            pc.putc(encrypted[i]);
        }

        pc.printf("\r\n\r\n");

        // Decrypt the 'encrypted' array and print.
        enigma.decrypt(decrypted, encrypted, strlen(message));
        decrypted[strlen(message)] = '\0';  // Terminate the c-style string (needed for 'printf').
        pc.printf("%s\r\n\r\n", decrypted);

        led1 = !led1;
        wait(2);
    }
}