Encryption library based on the Enigma machine. It's capable to encrypt any byte, not only the 26 capital letters of the alphabet.

Dependents:   Enigma_Hello

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

Committer:
hudakz
Date:
2019-09-24
Revision:
4:107a360171fe
Parent:
2:ee55abc2b6a2

File content as of revision 4:107a360171fe:

#ifndef ENIGMA_H
#define ENIGMA_H

#include "mbed.h"


class   Enigma
{
    size_t   _len;
    uint32_t _seed;
    uint8_t  _entryWheel[256];
    uint8_t* _rightRotor;
    uint8_t* _middleRotor;
    uint8_t* _leftRotor;
    uint8_t* _reflector;
    uint8_t  _leftRotorPos;
    uint8_t  _middleRotorPos;
    uint8_t  _rightRotorPos;
    uint8_t (*_plugboard)[2];
    uint8_t  _cords;

    size_t _mod(long i);
    int    _find(uint8_t byte, uint8_t* array);
public:
    static const uint8_t ROTOR_I[256];
    static const uint8_t ROTOR_II[256];
    static const uint8_t ROTOR_III[256];
    static const uint8_t ROTOR_IV[256];
    static const uint8_t ROTOR_V[256];
    static const uint8_t REFLECTOR[256];

    // Constructor
    Enigma
    (
        const uint8_t*  leftRotorSel,
        const uint8_t*  middleRotorSel,
        const uint8_t*  rightRotorSel,
        uint8_t         leftRotorPos,
        uint8_t         middleRotorPos,
        uint8_t         rightRotorPos,
        uint8_t         (*plugboard)[2],
        uint8_t         cords
    );

    // Methods
    uint8_t* encrypt(uint8_t* out, const uint8_t* in, size_t len);
    uint8_t* decrypt(uint8_t* out, const uint8_t* in, size_t len);

    // Helpers
    void   genRotorWiring(const char* name, unsigned seed);
    void   genReflectorWiring(unsigned seed);
};
#endif // ENIGMA_H