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: Enigma.cpp
- Revision:
- 1:a21920e248d3
- Parent:
- 0:7a702d2d6b54
- Child:
- 2:ee55abc2b6a2
--- a/Enigma.cpp Sun Sep 22 21:11:35 2019 +0000 +++ b/Enigma.cpp Mon Sep 23 07:50:09 2019 +0000 @@ -143,22 +143,23 @@ _entryWheel[i] = i; } - // Selected three rotors from the set of fives. + // Select three rotors out of fives. _leftRotor = const_cast<uint8_t*>(leftRotorSel); _middleRotor = const_cast<uint8_t*>(middleRotorSel); _rightRotor = const_cast<uint8_t*>(rightRotorSel); - // Initialize rflector. + // Initialize reflector. _reflector = const_cast<uint8_t*>(REFLECTOR); } /** - * @brief - * @note - * @param - * @retval + * @brief Encrypts a byte array. + * @note Encryption capability is extended from 26 chars to all 256 bytes. + * @retval out Pointer to a byte array to store the encrypted bytes in. + * @param in Pointer to a byte array to be encrypted. + * @param len Length of the byte array. */ -char* Enigma::encrypt(char* out, const char* in, size_t len) +uint8_t* Enigma::encrypt(uint8_t* out, const uint8_t* in, size_t len) { // Sets initial permutation int leftRotorPos = _leftRotorPos; @@ -166,55 +167,57 @@ int rightRotorPos = _rightRotorPos; for (size_t i = 0; i < len; i++) { - char c = in[i]; + uint8_t byte = in[i]; - // Avance right rotor on every character entry + // Advance right rotor on every byte entry rightRotorPos = _mod(rightRotorPos + 1); - // Avance mid rotor with a notch located at 13 + // Avance mid rotor using a notch located at position #13 if ((rightRotorPos % 13) == 0) { middleRotorPos = _mod(middleRotorPos + 2); } - // Avance left rotor with a notch located at 13 + // Advance left rotor using a notch located at position #13 if ((middleRotorPos % 13) == 0) { leftRotorPos = _mod(leftRotorPos + 1); } // Pass through rotors - c = _rightRotor[_mod(c + rightRotorPos)]; - c = _middleRotor[_mod(c + middleRotorPos - rightRotorPos)]; - c = _leftRotor[_mod(c + leftRotorPos - middleRotorPos)]; + byte = _rightRotor[_mod(byte + rightRotorPos)]; + byte = _middleRotor[_mod(byte + middleRotorPos - rightRotorPos)]; + byte = _leftRotor[_mod(byte + leftRotorPos - middleRotorPos)]; // Pass through reflector - c = _reflector[_mod(c - leftRotorPos)]; - c = _entryWheel[_mod(c + leftRotorPos)]; + byte = _reflector[_mod(byte - leftRotorPos)]; + byte = _entryWheel[_mod(byte + leftRotorPos)]; // Inverse pass trough rotors - c = _entryWheel[_mod(_find(c, _leftRotor) - leftRotorPos + middleRotorPos)]; - c = _entryWheel[_mod(_find(c, _middleRotor) - middleRotorPos + rightRotorPos)]; - c = _entryWheel[_mod(_find(c, _rightRotor) - rightRotorPos)]; + byte = _entryWheel[_mod(_find(byte, _leftRotor) - leftRotorPos + middleRotorPos)]; + byte = _entryWheel[_mod(_find(byte, _middleRotor) - middleRotorPos + rightRotorPos)]; + byte = _entryWheel[_mod(_find(byte, _rightRotor) - rightRotorPos)]; - out[i] = c; + out[i] = byte; } return out; } /** - * @brief - * @note - * @param - * @retval + * @brief Decrypts a byte array. + * @note Decryption capability is extended from 26 chars to all 256 bytes. + * @retval out Pointer to a byte array to store the decrypted bytes in. + * @param in Pointer to a byte array to be decrypted. + * @param len Length of the byte array. */ -char* Enigma::decrypt(char* out, const char* in, size_t len) +uint8_t* Enigma::decrypt(uint8_t* out, const uint8_t* in, size_t len) { return encrypt(out, in, len); } /** * @brief Generates a new rotor wiring. - * @note Prints to the serial terminal. + * @note Prints to the serial terminal. The generated byte array + * shall be then copy & pasted to his source code and recompiled. * @param name Rotor's name. Should be "I", "II", "III", "IV" or "V" * @param seed A number to seed the pseudo-ramdom generator with. * @retval @@ -263,7 +266,8 @@ /** * @brief Generates a reflector wiring. - * @note Prints to the serial terminal. + * @note Prints to the serial terminal. The generated byte array + * shall be then copy & pasted to his source code and recompiled. * @param seed A number to seed the pseudo-ramdom generator with. * @retval */