Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:ad5fb6f2ab3c
- Parent:
- 0:3381509d20af
- Child:
- 2:ac7a3cea1757
diff -r 3381509d20af -r ad5fb6f2ab3c main.cpp
--- a/main.cpp Sun Sep 22 21:14:33 2019 +0000
+++ b/main.cpp Mon Sep 23 07:50:57 2019 +0000
@@ -3,7 +3,7 @@
Serial pc(USBTX, USBRX);
DigitalOut led1(LED1);
-Enigma enigma(Enigma::ROTOR_IV, Enigma::ROTOR_I, Enigma::ROTOR_III, 126, 247, 14); // Selects rotors and sets initial position
+Enigma enigma(Enigma::ROTOR_IV, Enigma::ROTOR_I, Enigma::ROTOR_III, 126, 247, 14);
/**
* @brief
@@ -13,28 +13,30 @@
*/
int main()
{
- char message[] = "Hello World!"; // A message to encrypt and then decript
- char* encrypted = new char[strlen(message)];
- char* decrypted = new char[strlen(message) + 1];
+ 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];
-// enigma.genRotorWiring("IV", 213); // generates new wiring for rotor IV
-// enigma.genReflectorWiring(6); // generates new wiring for the reflector
+ // 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");
- pc.printf("%s\r\n", message);
+ pc.printf("------------------------------\r\n");
+ pc.printf("%s\r\n\r\n", message);
- enigma.encrypt(encrypted, message, strlen(message));
+ // Encrypt and print the encrypted message.
+ 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");
- pc.printf("\r\n");
-
+ // Decrypt the 'encrypted' array and print.
enigma.decrypt(decrypted, encrypted, strlen(message));
- decrypted[strlen(message)] = '\0';
- pc.printf("%s\r\n", decrypted);
-
+ decrypted[strlen(message)] = '\0'; // Terminate the c-style string (needed for 'printf').
+ pc.printf("%s\r\n\r\n", decrypted);
+
led1 = !led1;
wait(2);
}