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-22
Revision:
0:7a702d2d6b54
Child:
1:a21920e248d3

File content as of revision 0:7a702d2d6b54:

#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;

    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
    );

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

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