Encryption library based on the Enigma machine. It's capable to encrypt any byte, not only the 26 capital letters of the alphabet.
Encryption library based on the Enigma machine. It is capable to encrypt any byte, not only the 26 capital letters of the alphabet as the original Enigma machine did.
Combining three rotors from a set of five, each of the 3 rotor setting with 256 positions, and the plugboard with 120 pairs of plugs connected it has staggering (5 * 4 * 3) * 256^3 * [256! / ((256 - 2 * 120)! * 120! * 2^120)] = 4.6415160521069700910027304860568e+267
different settings. In other words, it uses a 889-bit key for encryption & decryption of data.
Enigma.h@4:107a360171fe, 2019-09-24 (annotated)
- Committer:
- hudakz
- Date:
- Tue Sep 24 16:56:56 2019 +0000
- Revision:
- 4:107a360171fe
- Parent:
- 2:ee55abc2b6a2
Encryption library based on the Enigma machine. It's capable to encrypt any byte, not only the 26 capital letters of the alphabet.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:7a702d2d6b54 | 1 | #ifndef ENIGMA_H |
hudakz | 0:7a702d2d6b54 | 2 | #define ENIGMA_H |
hudakz | 0:7a702d2d6b54 | 3 | |
hudakz | 0:7a702d2d6b54 | 4 | #include "mbed.h" |
hudakz | 0:7a702d2d6b54 | 5 | |
hudakz | 2:ee55abc2b6a2 | 6 | |
hudakz | 0:7a702d2d6b54 | 7 | class Enigma |
hudakz | 0:7a702d2d6b54 | 8 | { |
hudakz | 0:7a702d2d6b54 | 9 | size_t _len; |
hudakz | 0:7a702d2d6b54 | 10 | uint32_t _seed; |
hudakz | 0:7a702d2d6b54 | 11 | uint8_t _entryWheel[256]; |
hudakz | 0:7a702d2d6b54 | 12 | uint8_t* _rightRotor; |
hudakz | 0:7a702d2d6b54 | 13 | uint8_t* _middleRotor; |
hudakz | 0:7a702d2d6b54 | 14 | uint8_t* _leftRotor; |
hudakz | 0:7a702d2d6b54 | 15 | uint8_t* _reflector; |
hudakz | 0:7a702d2d6b54 | 16 | uint8_t _leftRotorPos; |
hudakz | 0:7a702d2d6b54 | 17 | uint8_t _middleRotorPos; |
hudakz | 0:7a702d2d6b54 | 18 | uint8_t _rightRotorPos; |
hudakz | 2:ee55abc2b6a2 | 19 | uint8_t (*_plugboard)[2]; |
hudakz | 2:ee55abc2b6a2 | 20 | uint8_t _cords; |
hudakz | 0:7a702d2d6b54 | 21 | |
hudakz | 0:7a702d2d6b54 | 22 | size_t _mod(long i); |
hudakz | 0:7a702d2d6b54 | 23 | int _find(uint8_t byte, uint8_t* array); |
hudakz | 0:7a702d2d6b54 | 24 | public: |
hudakz | 0:7a702d2d6b54 | 25 | static const uint8_t ROTOR_I[256]; |
hudakz | 0:7a702d2d6b54 | 26 | static const uint8_t ROTOR_II[256]; |
hudakz | 0:7a702d2d6b54 | 27 | static const uint8_t ROTOR_III[256]; |
hudakz | 0:7a702d2d6b54 | 28 | static const uint8_t ROTOR_IV[256]; |
hudakz | 0:7a702d2d6b54 | 29 | static const uint8_t ROTOR_V[256]; |
hudakz | 0:7a702d2d6b54 | 30 | static const uint8_t REFLECTOR[256]; |
hudakz | 0:7a702d2d6b54 | 31 | |
hudakz | 2:ee55abc2b6a2 | 32 | // Constructor |
hudakz | 0:7a702d2d6b54 | 33 | Enigma |
hudakz | 0:7a702d2d6b54 | 34 | ( |
hudakz | 0:7a702d2d6b54 | 35 | const uint8_t* leftRotorSel, |
hudakz | 0:7a702d2d6b54 | 36 | const uint8_t* middleRotorSel, |
hudakz | 0:7a702d2d6b54 | 37 | const uint8_t* rightRotorSel, |
hudakz | 0:7a702d2d6b54 | 38 | uint8_t leftRotorPos, |
hudakz | 0:7a702d2d6b54 | 39 | uint8_t middleRotorPos, |
hudakz | 2:ee55abc2b6a2 | 40 | uint8_t rightRotorPos, |
hudakz | 2:ee55abc2b6a2 | 41 | uint8_t (*plugboard)[2], |
hudakz | 2:ee55abc2b6a2 | 42 | uint8_t cords |
hudakz | 0:7a702d2d6b54 | 43 | ); |
hudakz | 0:7a702d2d6b54 | 44 | |
hudakz | 2:ee55abc2b6a2 | 45 | // Methods |
hudakz | 1:a21920e248d3 | 46 | uint8_t* encrypt(uint8_t* out, const uint8_t* in, size_t len); |
hudakz | 1:a21920e248d3 | 47 | uint8_t* decrypt(uint8_t* out, const uint8_t* in, size_t len); |
hudakz | 0:7a702d2d6b54 | 48 | |
hudakz | 2:ee55abc2b6a2 | 49 | // Helpers |
hudakz | 0:7a702d2d6b54 | 50 | void genRotorWiring(const char* name, unsigned seed); |
hudakz | 0:7a702d2d6b54 | 51 | void genReflectorWiring(unsigned seed); |
hudakz | 0:7a702d2d6b54 | 52 | }; |
hudakz | 0:7a702d2d6b54 | 53 | #endif // ENIGMA_H |