Example of using the Enigma encryption library.

Dependencies:   Enigma

Committer:
hudakz
Date:
Tue Sep 24 16:58:11 2019 +0000
Revision:
3:83f583d06005
Parent:
2:ac7a3cea1757
Example of using the Enigma encryption library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:3381509d20af 1 #include "mbed.h"
hudakz 0:3381509d20af 2 #include "Enigma.h"
hudakz 0:3381509d20af 3
hudakz 0:3381509d20af 4 Serial pc(USBTX, USBRX);
hudakz 0:3381509d20af 5 DigitalOut led1(LED1);
hudakz 2:ac7a3cea1757 6 uint8_t plugboard[][2] = { { 4, 183 }, { 72, 247 }, { 108, 192 } }; // Define pairs of plugs connected with cords.
hudakz 2:ac7a3cea1757 7 Enigma enigma(Enigma::ROTOR_IV, Enigma::ROTOR_I, Enigma::ROTOR_III, 126, 247, 14, plugboard, 3);
hudakz 3:83f583d06005 8 Timer timer;
hudakz 3:83f583d06005 9 time_t timeElapsed;
hudakz 0:3381509d20af 10
hudakz 0:3381509d20af 11 /**
hudakz 0:3381509d20af 12 * @brief
hudakz 0:3381509d20af 13 * @note
hudakz 0:3381509d20af 14 * @param
hudakz 0:3381509d20af 15 * @retval
hudakz 0:3381509d20af 16 */
hudakz 0:3381509d20af 17 int main()
hudakz 0:3381509d20af 18 {
hudakz 3:83f583d06005 19 char message[] = // A message to encrypt.
hudakz 3:83f583d06005 20 "The Enigma machine is an encryption device developed and used\r\n"
hudakz 3:83f583d06005 21 "in the early- to mid-20th century to protect commercial, diplomatic\r\n"
hudakz 3:83f583d06005 22 "and military communication.\r\n"
hudakz 3:83f583d06005 23 "It was employed extensively by Nazi Germany during World War II,\r\n"
hudakz 3:83f583d06005 24 "in all branches of the German military.";
hudakz 1:ad5fb6f2ab3c 25 uint8_t* encrypted = new uint8_t[strlen(message)];
hudakz 1:ad5fb6f2ab3c 26 uint8_t* decrypted = new uint8_t[strlen(message) + 1];
hudakz 0:3381509d20af 27
hudakz 1:ad5fb6f2ab3c 28 // Helper functions for maintenance only:
hudakz 1:ad5fb6f2ab3c 29 //enigma.genRotorWiring("IV", 213); // Generate new wiring for rotor IV.
hudakz 1:ad5fb6f2ab3c 30 //enigma.genReflectorWiring(6); // Generate new wiring for the reflector.
hudakz 3:83f583d06005 31
hudakz 3:83f583d06005 32 // Print the 'message'.
hudakz 3:83f583d06005 33 pc.printf("%s\r\n\r\n", message);
hudakz 2:ac7a3cea1757 34
hudakz 3:83f583d06005 35 // Encrypt the 'message' to a byte array and print.
hudakz 3:83f583d06005 36 timer.start();
hudakz 3:83f583d06005 37 enigma.encrypt(encrypted, (uint8_t*)message, strlen(message));
hudakz 3:83f583d06005 38 timeElapsed = timer.read_ms();
hudakz 3:83f583d06005 39 pc.printf("It took %d ms to encrypt the message:\r\n\r\n", timeElapsed);
hudakz 3:83f583d06005 40 for (size_t i = 0; i < strlen(message); i++) {
hudakz 3:83f583d06005 41 if (i % 16 == 15) {
hudakz 3:83f583d06005 42 printf("0x%.2X\r\n", encrypted[i]);
hudakz 3:83f583d06005 43 }
hudakz 3:83f583d06005 44 else {
hudakz 3:83f583d06005 45 printf("0x%.2X ", encrypted[i]);
hudakz 3:83f583d06005 46 }
hudakz 3:83f583d06005 47 }
hudakz 3:83f583d06005 48 printf("\r\n\r\n");
hudakz 0:3381509d20af 49
hudakz 3:83f583d06005 50 // Decrypt the 'encrypted' byte array and print.
hudakz 3:83f583d06005 51 timer.reset();
hudakz 3:83f583d06005 52 enigma.decrypt(decrypted, encrypted, strlen(message));
hudakz 3:83f583d06005 53 timeElapsed = timer.read_ms();
hudakz 3:83f583d06005 54 pc.printf("It took %d ms to decrypt the message:\r\n\r\n", timeElapsed);
hudakz 3:83f583d06005 55 decrypted[strlen(message)] = '\0'; // Terminate the c-style string (needed for 'printf').
hudakz 3:83f583d06005 56 pc.printf("%s\r\n\r\n", decrypted);
hudakz 3:83f583d06005 57
hudakz 3:83f583d06005 58 return 0;
hudakz 0:3381509d20af 59 }