Zoltan Hudak / Enigma

Dependents:   Enigma_Hello

Enigma.cpp

Committer:
hudakz
Date:
2019-09-23
Revision:
1:a21920e248d3
Parent:
0:7a702d2d6b54
Child:
2:ee55abc2b6a2

File content as of revision 1:a21920e248d3:

#include "Enigma.h"

/*$off*/
// Internal wiring of the five available rotors
const uint8_t Enigma::ROTOR_I[256] = {
    0xe0, 0xd7, 0xf1, 0x7a, 0x92, 0x84, 0x5e, 0x1b, 0xd6, 0x38, 0x79, 0x10, 0x7c, 0xa6, 0x1f, 0x4d,
    0x4f, 0x30, 0x19, 0xdd, 0x61, 0x00, 0xa2, 0x82, 0x98, 0xf9, 0x2f, 0x6a, 0xc5, 0x64, 0x9d, 0x9b,
    0xca, 0x17, 0xd4, 0x24, 0x2b, 0x20, 0xd5, 0xea, 0xba, 0x25, 0x2e, 0x08, 0x78, 0x32, 0xaf, 0xe6,
    0x4c, 0x13, 0x56, 0xbd, 0xd9, 0xe5, 0x33, 0xee, 0x69, 0xf3, 0x0f, 0x99, 0xbb, 0x4e, 0x01, 0x77,
    0x9c, 0x91, 0xf0, 0x89, 0xf8, 0x74, 0xb1, 0x95, 0x88, 0x42, 0xd0, 0x50, 0xfc, 0xa7, 0x7e, 0x7d,
    0x59, 0x73, 0x6d, 0x45, 0x5b, 0x3d, 0x3b, 0x34, 0x97, 0xed, 0xde, 0x47, 0xf4, 0xe1, 0x60, 0xe4,
    0x2a, 0xad, 0xfb, 0x81, 0xa9, 0x29, 0x57, 0x51, 0xc4, 0x3f, 0x53, 0x1d, 0xbe, 0x06, 0xfa, 0x85,
    0x12, 0x3a, 0x8a, 0x96, 0x63, 0x07, 0x8c, 0xd3, 0x72, 0xe3, 0x54, 0xcb, 0x4b, 0x35, 0x0b, 0xeb,
    0xda, 0xd2, 0x3e, 0x7f, 0x1a, 0xb6, 0x0c, 0xc0, 0xb2, 0xc8, 0xa8, 0x66, 0x39, 0x8e, 0xf5, 0xa4,
    0xdf, 0xe2, 0x8d, 0x44, 0x31, 0x7b, 0x14, 0xdc, 0xa3, 0xc3, 0xfd, 0x1e, 0xef, 0x23, 0x9a, 0x2d,
    0xf7, 0xb8, 0xc1, 0x94, 0x36, 0x5d, 0x48, 0x68, 0x80, 0xec, 0x4a, 0x41, 0xb4, 0xe8, 0x16, 0x6e,
    0x49, 0x43, 0x18, 0x11, 0x9f, 0xd8, 0x8b, 0x83, 0xcc, 0x03, 0xcf, 0x5c, 0x27, 0xc6, 0x02, 0xe7,
    0xcd, 0x70, 0x5a, 0x04, 0xff, 0xc7, 0xb0, 0x76, 0xb7, 0x26, 0xb3, 0xce, 0x15, 0x6c, 0xb5, 0x0d,
    0x93, 0x09, 0xf2, 0xdb, 0xbc, 0x37, 0xaa, 0x87, 0xf6, 0xab, 0x67, 0x21, 0x28, 0xc2, 0xc9, 0x52,
    0x0e, 0x5f, 0x58, 0xae, 0x22, 0x6f, 0x86, 0x2c, 0x0a, 0x1c, 0x62, 0x9e, 0xa5, 0x46, 0x05, 0xac,
    0x65, 0x40, 0x75, 0x6b, 0xb9, 0xe9, 0xd1, 0x8f, 0x71, 0xfe, 0x55, 0xa0, 0xa1, 0x3c, 0x90, 0xbf
};

const uint8_t Enigma::ROTOR_II[256] = {
    0xff, 0xa2, 0x4f, 0x1a, 0x1b, 0x20, 0x9b, 0xec, 0x47, 0x3e, 0xe7, 0x0d, 0x0f, 0x34, 0x50, 0x46,
    0x41, 0x9a, 0xf6, 0x63, 0xb6, 0x83, 0xea, 0x51, 0x88, 0xd4, 0xc1, 0x52, 0x42, 0x37, 0xc0, 0xc6,
    0xe3, 0x1e, 0xf7, 0xa9, 0x60, 0x4b, 0xda, 0x2d, 0x94, 0x7f, 0x85, 0x2f, 0xa0, 0x6a, 0xfa, 0xc8,
    0xde, 0x06, 0x8d, 0x75, 0xf8, 0xee, 0xaa, 0xc7, 0xef, 0x38, 0x14, 0x69, 0x23, 0x05, 0x8c, 0x3d,
    0x5d, 0xc9, 0x13, 0x2e, 0xab, 0x6c, 0x32, 0xc5, 0xfc, 0xb4, 0xb0, 0x98, 0x1d, 0xbc, 0x2a, 0xa6,
    0x3f, 0xf2, 0xac, 0xc4, 0x8e, 0xbb, 0xf9, 0x49, 0xad, 0x9c, 0xd5, 0x57, 0xeb, 0xa1, 0x3b, 0x15,
    0x7d, 0x28, 0x89, 0xb5, 0xb7, 0xe1, 0x5b, 0x65, 0xdc, 0x10, 0x82, 0x36, 0xb8, 0x2c, 0x25, 0x7a,
    0x91, 0x2b, 0xfe, 0x9f, 0x07, 0x01, 0xa4, 0x6f, 0xae, 0x79, 0xf5, 0xd9, 0x48, 0x5c, 0x6d, 0x74,
    0xb1, 0x8a, 0x58, 0x33, 0xd0, 0x6e, 0x40, 0xbd, 0x4d, 0x97, 0xcf, 0x62, 0x84, 0x30, 0x67, 0xa7,
    0xe6, 0xe2, 0xb2, 0x61, 0x80, 0x92, 0x0e, 0x68, 0x4e, 0x66, 0x81, 0x16, 0x90, 0x53, 0x11, 0xbe,
    0x6b, 0xd1, 0x04, 0xd3, 0x08, 0x70, 0x95, 0x93, 0x4c, 0xa5, 0xdf, 0xd6, 0x5e, 0xa8, 0x7e, 0x9e,
    0x19, 0xe0, 0x77, 0x73, 0xbf, 0xf1, 0x21, 0x9d, 0x55, 0xfd, 0xe8, 0x56, 0xdb, 0xce, 0x64, 0x43,
    0x0c, 0x29, 0x0b, 0x44, 0x31, 0xe9, 0x8f, 0x1f, 0x8b, 0x27, 0xed, 0x3a, 0xfb, 0x12, 0xca, 0x59,
    0x1c, 0xdd, 0xb3, 0x87, 0x3c, 0x96, 0x78, 0x86, 0x54, 0xd2, 0xcc, 0x26, 0x99, 0xc3, 0x71, 0x02,
    0xf0, 0xe4, 0x45, 0x22, 0x18, 0xd8, 0x5f, 0xd7, 0xb9, 0xcd, 0xba, 0x5a, 0x7b, 0xf4, 0xaf, 0xa3,
    0x00, 0x24, 0x35, 0x7c, 0x09, 0x39, 0x03, 0x17, 0xcb, 0xf3, 0x72, 0x4a, 0xe5, 0x76, 0xc2, 0x0a
};

const uint8_t Enigma::ROTOR_III[256] = {
    0x1c, 0xb8, 0x5a, 0xa6, 0xc2, 0xf9, 0x78, 0xf6, 0x41, 0x22, 0xd7, 0x1e, 0xad, 0xcc, 0x96, 0xe0,
    0x59, 0x69, 0x09, 0x73, 0xe8, 0xfc, 0x1a, 0x17, 0xa2, 0x12, 0xfe, 0x00, 0xfd, 0x7e, 0x0c, 0x60,
    0x54, 0x8d, 0x2e, 0xfa, 0x9e, 0xb1, 0xa5, 0x3f, 0x03, 0x7b, 0xb9, 0xbf, 0x90, 0xdd, 0xe5, 0xd2,
    0x2f, 0x99, 0xcd, 0x80, 0x79, 0xcb, 0xc6, 0x5e, 0x31, 0x08, 0x29, 0xea, 0xd9, 0x58, 0xf2, 0x4f,
    0x4e, 0x02, 0xc0, 0x98, 0x34, 0x47, 0x64, 0xb4, 0xce, 0x5c, 0x8c, 0xa4, 0x28, 0x83, 0xcf, 0x44,
    0xec, 0xc9, 0xf4, 0x30, 0x56, 0xb2, 0x1b, 0x8f, 0x07, 0xff, 0x35, 0x6e, 0x0f, 0x7c, 0xae, 0x11,
    0xab, 0xe1, 0x13, 0xa0, 0xc8, 0xe9, 0x2c, 0x70, 0x82, 0x91, 0xee, 0x76, 0xbb, 0xde, 0x20, 0x7f,
    0xf0, 0x5b, 0xe4, 0x36, 0xa7, 0x2d, 0x89, 0x53, 0x93, 0x24, 0xa1, 0x3e, 0x51, 0xd0, 0xf8, 0x95,
    0xdb, 0x77, 0x8a, 0x40, 0x8b, 0xc3, 0xdc, 0x3d, 0xd4, 0x9f, 0x19, 0xbe, 0xd3, 0xe7, 0x45, 0x10,
    0x8e, 0x9d, 0x0d, 0x57, 0xa9, 0x72, 0x21, 0x49, 0x52, 0x04, 0xd8, 0xf1, 0xeb, 0x4c, 0xb0, 0x14,
    0x87, 0x55, 0x33, 0xa8, 0x61, 0x38, 0x39, 0x65, 0x42, 0x0e, 0xe2, 0xb5, 0x4b, 0x94, 0x27, 0xf7,
    0x06, 0x48, 0xd6, 0x6d, 0xb3, 0x50, 0x85, 0x6a, 0xbd, 0x3c, 0xdf, 0x6b, 0x67, 0x01, 0xfb, 0x86,
    0x63, 0xf3, 0xbc, 0x23, 0x88, 0xf5, 0xca, 0x2b, 0x18, 0xc4, 0x3b, 0x5f, 0x84, 0x7d, 0xed, 0xda,
    0x1d, 0xd1, 0x74, 0x0a, 0x43, 0x75, 0xc7, 0x32, 0x62, 0xb6, 0x16, 0xe6, 0x4a, 0xa3, 0x37, 0x71,
    0x81, 0x26, 0xaf, 0x68, 0x2a, 0x9a, 0x5d, 0x3a, 0x7a, 0xb7, 0xe3, 0x46, 0x0b, 0xd5, 0x25, 0x4d,
    0xc1, 0x66, 0xaa, 0x1f, 0x05, 0xba, 0x9c, 0x6c, 0x9b, 0xac, 0x97, 0x6f, 0xef, 0xc5, 0x92, 0x15
};

const uint8_t Enigma::ROTOR_IV[256] = {
    0xc1, 0x1b, 0x04, 0xe6, 0x4a, 0x89, 0xaa, 0xbe, 0x78, 0x05, 0x8f, 0x1a, 0x02, 0x81, 0x45, 0x3a,
    0xaf, 0xee, 0x68, 0xd4, 0x71, 0x5d, 0x92, 0x90, 0x36, 0x0d, 0x21, 0x14, 0x46, 0xbc, 0x09, 0xb2,
    0xef, 0x8d, 0xba, 0x8c, 0xac, 0xdd, 0x01, 0xd8, 0xda, 0xcf, 0xff, 0xfc, 0xf6, 0x48, 0xf2, 0xb9,
    0x95, 0x59, 0xc4, 0x76, 0xb6, 0xca, 0x58, 0x2c, 0x07, 0xec, 0x2a, 0x47, 0x99, 0x8a, 0x31, 0x20,
    0xe2, 0x7f, 0x30, 0xbd, 0x8e, 0xb5, 0x70, 0x0b, 0x18, 0x37, 0x75, 0xb1, 0x94, 0xc7, 0x06, 0x00,
    0x73, 0xc8, 0xcc, 0x9e, 0xe9, 0x2d, 0x1d, 0x44, 0x98, 0x1f, 0x93, 0xfb, 0x41, 0x50, 0xad, 0x32,
    0x61, 0xbb, 0xf1, 0xfe, 0x24, 0xce, 0xb4, 0xab, 0x72, 0xea, 0x25, 0x67, 0xcd, 0x53, 0x26, 0x7a,
    0x77, 0xa8, 0x23, 0x19, 0x79, 0xd1, 0x9c, 0x5f, 0xf5, 0xc0, 0x1c, 0x5b, 0xe3, 0xc6, 0x6e, 0xd5,
    0xe0, 0x10, 0x3d, 0x66, 0x7d, 0x33, 0x51, 0x6c, 0x82, 0xd6, 0x84, 0x40, 0xdb, 0xe8, 0xc9, 0x62,
    0xf3, 0x5c, 0xd3, 0x60, 0x4d, 0x96, 0x08, 0x8b, 0xfa, 0xd0, 0xb7, 0x7e, 0xc2, 0x55, 0xb8, 0x63,
    0x86, 0x2f, 0x97, 0xa9, 0xa6, 0x3e, 0x34, 0xcb, 0x0e, 0x38, 0x6d, 0xbf, 0xf7, 0x11, 0x6b, 0x0a,
    0xeb, 0xf0, 0x15, 0x0c, 0xb3, 0x12, 0x88, 0xa4, 0x69, 0xd2, 0x9f, 0xa1, 0x9a, 0x39, 0x27, 0x2b,
    0x4f, 0x03, 0x91, 0xa5, 0x3b, 0xde, 0xf9, 0x4b, 0x28, 0x9b, 0x1e, 0x17, 0xe7, 0x6f, 0xdf, 0x16,
    0x43, 0xf4, 0xdc, 0x7c, 0x87, 0x54, 0xe4, 0xa3, 0x52, 0x0f, 0xe5, 0x49, 0xed, 0x56, 0xfd, 0xe1,
    0x13, 0x22, 0x85, 0x9d, 0x4c, 0xd9, 0x65, 0x3c, 0x6a, 0x5e, 0xb0, 0xa0, 0x29, 0x2e, 0x42, 0x35,
    0xae, 0x5a, 0x4e, 0xa7, 0xc3, 0x7b, 0xa2, 0x83, 0x64, 0x74, 0xf8, 0xd7, 0x80, 0x3f, 0xc5, 0x57
};

const uint8_t Enigma::ROTOR_V[256] = {
    0xfe, 0xc8, 0x49, 0x21, 0xa5, 0x5a, 0xf4, 0xa8, 0xc3, 0x1b, 0x9a, 0x5e, 0x22, 0xae, 0x7f, 0xea,
    0x8f, 0xf1, 0x15, 0x73, 0xe3, 0xce, 0x05, 0x3c, 0x08, 0x07, 0xeb, 0x96, 0xcf, 0x98, 0x29, 0xbd,
    0x03, 0xad, 0x9e, 0x32, 0x0e, 0xc1, 0xb9, 0xd2, 0x83, 0xd6, 0x4d, 0x14, 0x1c, 0x68, 0x9d, 0x31,
    0xa2, 0x97, 0x01, 0x7e, 0xe5, 0xf6, 0x2d, 0x27, 0xe8, 0xf5, 0xf9, 0xb2, 0x10, 0x82, 0xd8, 0x66,
    0x76, 0x8e, 0x19, 0xfd, 0xa3, 0xa6, 0x8a, 0xd0, 0x2f, 0xc9, 0xb4, 0x9c, 0xd7, 0x38, 0xdf, 0x99,
    0xef, 0xc4, 0x09, 0xb1, 0x13, 0x3a, 0x6d, 0xbc, 0x58, 0x7c, 0x4c, 0x11, 0x7d, 0xe2, 0x30, 0xc7,
    0x2b, 0x6b, 0xab, 0x6e, 0x63, 0x7b, 0xff, 0x9b, 0x57, 0xd4, 0x61, 0x51, 0x26, 0x37, 0x4a, 0xed,
    0xb6, 0xac, 0xc5, 0x92, 0x89, 0x91, 0xcc, 0x1f, 0x0c, 0xa0, 0x6f, 0x94, 0xec, 0xa4, 0xdc, 0x4f,
    0x8b, 0xdb, 0x6c, 0x40, 0xc6, 0x48, 0x54, 0x45, 0x16, 0xe9, 0x6a, 0x20, 0x52, 0x67, 0x55, 0x34,
    0x90, 0x79, 0xf0, 0x33, 0x9f, 0xda, 0x87, 0xf2, 0x85, 0xf3, 0x00, 0xf7, 0xbf, 0x69, 0xa9, 0x50,
    0x59, 0x36, 0xe0, 0x3d, 0x06, 0x0f, 0x46, 0x44, 0xb5, 0x56, 0xe7, 0x77, 0x3b, 0x5c, 0x0b, 0xe4,
    0x35, 0xc2, 0x72, 0x41, 0x2e, 0x0d, 0xaf, 0xf8, 0xc0, 0x0a, 0x17, 0x24, 0x95, 0x81, 0xee, 0x1d,
    0x2a, 0xa7, 0xcb, 0xd1, 0x12, 0x02, 0xb7, 0x42, 0x84, 0x4e, 0x28, 0x78, 0x8c, 0x1a, 0xb0, 0x5b,
    0x7a, 0xfa, 0xde, 0xbb, 0x93, 0x88, 0xcd, 0xe1, 0xfc, 0x04, 0x1e, 0x43, 0x8d, 0x74, 0x80, 0x25,
    0x47, 0xa1, 0xaa, 0x39, 0xdd, 0xb3, 0xd5, 0x3f, 0x65, 0x5d, 0xba, 0x18, 0x71, 0x60, 0x75, 0x62,
    0x2c, 0x4b, 0x53, 0xca, 0x64, 0xe6, 0x23, 0x5f, 0x3e, 0x86, 0x70, 0xfb, 0xd3, 0xd9, 0xbe, 0xb8
};

// Internal wiring of the reflector
const uint8_t Enigma::REFLECTOR[256] = {
    0x31, 0x74, 0x9c, 0xc4, 0x2a, 0x7e, 0xc6, 0x69, 0x19, 0xa3, 0x5d, 0x91, 0xe7, 0x15, 0x2f, 0xa1,
    0x29, 0x7c, 0xd1, 0x80, 0x9e, 0x0d, 0xf0, 0xda, 0xff, 0x08, 0xd5, 0xfc, 0xdf, 0xa0, 0x58, 0xaf,
    0x4d, 0xc3, 0x8f, 0x46, 0x97, 0x70, 0xcd, 0x48, 0xea, 0x10, 0x04, 0xd3, 0x44, 0xd6, 0x51, 0x0e,
    0xfe, 0x00, 0xa2, 0x9d, 0x59, 0x50, 0x98, 0x6c, 0x53, 0x8c, 0xd8, 0x92, 0xa8, 0x87, 0x84, 0x6f,
    0x6d, 0xb6, 0x9a, 0xe3, 0x2c, 0x79, 0x23, 0xa7, 0x27, 0xa9, 0x66, 0xdb, 0xb5, 0x20, 0x68, 0x5b,
    0x35, 0x2e, 0xb1, 0x38, 0x89, 0xc7, 0xf1, 0x96, 0x1e, 0x34, 0xcb, 0x4f, 0x83, 0x0a, 0xf9, 0xfb,
    0x6b, 0xe2, 0xd0, 0x93, 0x71, 0x75, 0x4a, 0x8d, 0x4e, 0x07, 0x86, 0x60, 0x37, 0x40, 0xcc, 0x3f,
    0x25, 0x64, 0xe4, 0xc1, 0x01, 0x65, 0xfa, 0xb4, 0xf4, 0x45, 0xb2, 0xa5, 0x11, 0x7f, 0x05, 0x7d,
    0x13, 0xc8, 0xdd, 0x5c, 0x3e, 0x99, 0x6a, 0x3d, 0xf7, 0x54, 0xad, 0xc9, 0x39, 0x67, 0xe8, 0x22,
    0xef, 0x0b, 0x3b, 0x63, 0xf2, 0xd4, 0x57, 0x24, 0x36, 0x85, 0x42, 0xab, 0x02, 0x33, 0x14, 0xed,
    0x1d, 0x0f, 0x32, 0x09, 0xd7, 0x7b, 0xb0, 0x47, 0x3c, 0x49, 0xfd, 0x9b, 0xe6, 0x8a, 0xe0, 0x1f,
    0xa6, 0x52, 0x7a, 0xbd, 0x77, 0x4c, 0x41, 0xbf, 0xf6, 0xbb, 0xd9, 0xb9, 0xdc, 0xb3, 0xc0, 0xb7,
    0xbe, 0x73, 0xd2, 0x21, 0x03, 0xce, 0x06, 0x55, 0x81, 0x8b, 0xe5, 0x5a, 0x6e, 0x26, 0xc5, 0xee,
    0x62, 0x12, 0xc2, 0x2b, 0x95, 0x1a, 0x2d, 0xa4, 0x3a, 0xba, 0x17, 0x4b, 0xbc, 0x82, 0xec, 0x1c,
    0xae, 0xf5, 0x61, 0x43, 0x72, 0xca, 0xac, 0x0c, 0x8e, 0xf3, 0x28, 0xf8, 0xde, 0x9f, 0xcf, 0x90,
    0x16, 0x56, 0x94, 0xe9, 0x78, 0xe1, 0xb8, 0x88, 0xeb, 0x5e, 0x76, 0x5f, 0x1b, 0xaa, 0x30, 0x18
};
/*$on*/

/**
 * @brief   Constructor
 * @note    Selects which rotors are going to be used and sets their intial position.
 * @param
 * @retval
 */
Enigma::Enigma
(
    const uint8_t*  leftRotorSel,
    const uint8_t*  middleRotorSel,
    const uint8_t*  rightRotorSel,
    uint8_t         leftRotorPos,
    uint8_t         middleRotorPos,
    uint8_t         rightRotorPos
) :
    _len(256),
    _leftRotorPos(leftRotorPos),
    _middleRotorPos(middleRotorPos),
    _rightRotorPos(rightRotorPos)
{
    // Initialize entry wheel.
    for (size_t i = 0; i < _len; i++) {
        _entryWheel[i] = i;
    }

    // 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 reflector.
    _reflector = const_cast<uint8_t*>(REFLECTOR);
}

/**
 * @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.
 */
uint8_t* Enigma::encrypt(uint8_t* out, const uint8_t* in, size_t len)
{
    // Sets initial permutation
    int leftRotorPos = _leftRotorPos;
    int middleRotorPos = _middleRotorPos;
    int rightRotorPos = _rightRotorPos;

    for (size_t i = 0; i < len; i++) {
        uint8_t byte = in[i];

        // Advance right rotor on every byte entry
        rightRotorPos = _mod(rightRotorPos + 1);

        // Avance mid rotor using a notch located at position #13
        if ((rightRotorPos % 13) == 0) {
            middleRotorPos = _mod(middleRotorPos + 2);
        }

        // Advance left rotor using a notch located at position #13
        if ((middleRotorPos % 13) == 0) {
            leftRotorPos = _mod(leftRotorPos + 1);
        }

        // Pass through rotors
        byte = _rightRotor[_mod(byte + rightRotorPos)];
        byte = _middleRotor[_mod(byte + middleRotorPos - rightRotorPos)];
        byte = _leftRotor[_mod(byte + leftRotorPos - middleRotorPos)];

        // Pass through reflector
        byte = _reflector[_mod(byte - leftRotorPos)];
        byte = _entryWheel[_mod(byte + leftRotorPos)];

        // Inverse pass trough rotors
        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] = byte;
    }

    return out;
}

/**
 * @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.
 */
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. 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
 */
void Enigma::genRotorWiring(const char* name, unsigned seed)
{
    uint8_t*    rotor = new uint8_t[_len];
    srand(seed);
    for (size_t i = 0; i < _len; i++) {
        rotor[i] = i;
    }
    for (int j = 0; j < (rand() % 1000 + 1000); j++) {
        for (size_t i = 0; i < _len; i++) {
            uint8_t byte = rotor[i];
            int     k = rand() % _len;
            rotor[i] = rotor[k];
            rotor[k] = byte;
        }
    }

    printf("const uint8_t Enigma::ROTOR_%s[256] = {\r\n", name);
    for (size_t i = 0; i < _len; i++) {
        if (i == 255) {
            printf("0x%.2x\n", rotor[i]);
        }
        else {
            switch (i % 16) {
                case 0:
                    printf("    0x%.2x, ", rotor[i]);
                    break;

                case 15:
                    printf("0x%.2x,\n", rotor[i]);
                    break;

                default:
                    printf("0x%.2x, ", rotor[i]);
            }
        }
    }

    printf("};\r\n\r\n");

    delete[] rotor;
}

/**
 * @brief   Generates a reflector wiring.
 * @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
 */
void Enigma::genReflectorWiring(unsigned seed)
{
    uint8_t*    reflector = new uint8_t[_len];
    uint8_t*    modified = new uint8_t[_len];
    size_t      k;

    srand(seed);
    for (size_t i = 0; i < _len; i++) {
        reflector[i] = i;
    }

    memset(modified, 0, _len);

    for (size_t i = 0; i < _len; i++) {
        if (modified[i] != 0) {
            continue;
        }

        do {
            k = i + 1 + rand() % (_len - i - 1);
        } while (modified[k]);

        uint8_t byte = reflector[k];
        reflector[k] = reflector[i];
        reflector[i] = byte;
        modified[i] = 1;
        modified[k] = 1;
    }

    delete[] modified;

    printf("const uint8_t Enigma::REFLECTOR[256] = {\r\n");
    for (size_t i = 0; i < _len; i++) {
        if (i == 255) {
            printf("0x%.2x\n", reflector[i]);
        }
        else {
            switch (i % 16) {
                case 0:
                    printf("    0x%.2x, ", reflector[i]);
                    break;

                case 15:
                    printf("0x%.2x,\n", reflector[i]);
                    break;

                default:
                    printf("0x%.2x, ", reflector[i]);
            }
        }
    }

    printf("};\r\n\r\n");

    delete[] reflector;
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
size_t Enigma::_mod(long i)
{
    return((i % _len + _len) % _len);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int Enigma::_find(uint8_t byte, uint8_t* array)
{
    for (size_t i = 0; i < _len; i++) {
        if (array[i] == byte) {
            return i;
        }
    }

    return -1;
}