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:
- 2:ee55abc2b6a2
- Parent:
- 1:a21920e248d3
- Child:
- 3:9590c44c9ecd
diff -r a21920e248d3 -r ee55abc2b6a2 Enigma.cpp --- a/Enigma.cpp Mon Sep 23 07:50:09 2019 +0000 +++ b/Enigma.cpp Tue Sep 24 08:52:11 2019 +0000 @@ -121,6 +121,7 @@ /** * @brief Constructor * @note Selects which rotors are going to be used and sets their intial position. + * @note Also connects pairs of plugs on the plugboard with cords. * @param * @retval */ @@ -131,12 +132,15 @@ const uint8_t* rightRotorSel, uint8_t leftRotorPos, uint8_t middleRotorPos, - uint8_t rightRotorPos + uint8_t rightRotorPos, + uint8_t (*plugboard)[2], + uint8_t cords ) : _len(256), _leftRotorPos(leftRotorPos), _middleRotorPos(middleRotorPos), - _rightRotorPos(rightRotorPos) + _rightRotorPos(rightRotorPos), + _cords(cords) { // Initialize entry wheel. for (size_t i = 0; i < _len; i++) { @@ -150,17 +154,26 @@ // Initialize reflector. _reflector = const_cast<uint8_t*>(REFLECTOR); + + _plugboard = new uint8_t[cords][2]; + for (size_t i = 0; i < cords; i++) { + for (size_t j = 0; j < 2; j++) { + _plugboard[i][j] = plugboard[i][j]; + } + } } /** * @brief Encrypts a byte array. - * @note Encryption capability is extended from 26 chars to all 256 bytes. + * @note Encryption capability is extended from 26 upper case letters to any byte. * @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. */ uint8_t* Enigma::encrypt(uint8_t* out, const uint8_t* in, size_t len) { + bool found; + // Sets initial permutation int leftRotorPos = _leftRotorPos; int middleRotorPos = _middleRotorPos; @@ -169,6 +182,21 @@ for (size_t i = 0; i < len; i++) { uint8_t byte = in[i]; + // Swap bytes connected with cords + found = false; + for (size_t j = 0; j < _cords; j++) { + for (size_t k = 0; k < 2; k++) { + if (_plugboard[j][k] == byte) { + byte = k == 0 ? _plugboard[j][1] : _plugboard[j][0]; + found = true; + break; + } + } + if (found) { + break; + } + } + // Advance right rotor on every byte entry rightRotorPos = _mod(rightRotorPos + 1); @@ -177,8 +205,8 @@ middleRotorPos = _mod(middleRotorPos + 2); } - // Advance left rotor using a notch located at position #13 - if ((middleRotorPos % 13) == 0) { + // Advance left rotor using a notch located at position #201 + if ((middleRotorPos % 201) == 0) { leftRotorPos = _mod(leftRotorPos + 1); } @@ -196,6 +224,21 @@ byte = _entryWheel[_mod(_find(byte, _middleRotor) - middleRotorPos + rightRotorPos)]; byte = _entryWheel[_mod(_find(byte, _rightRotor) - rightRotorPos)]; + // Swap bytes connected with cords + found = false; + for (size_t j = 0; j < _cords; j++) { + for (size_t k = 0; k < 2; k++) { + if (_plugboard[j][k] == byte) { + byte = k == 0 ? _plugboard[j][1] : _plugboard[j][0]; + found = true; + break; + } + } + if (found) { + break; + } + } + out[i] = byte; }