The Machine
class allows anyone to encode and decode messages with Enigma. Decoding is done by simply passing the encoded message through a machine with the original configurations.
var enigma = require('./enigma');
// Creating Enigma machine
var machine = new enigma.Machine();
// Setting up the plugboard
machine.setPlugboard(new enigma.Plugboard('MA', 'VB', 'PF'));
// Setting up the rotors
var rightRotor = new enigma.RotorIII();
rightRotor.setInnerPosition('B');
rightRotor.setInitialPosition('J');
var middleRotor = new enigma.RotorII();
middleRotor.setInnerPosition('W');
middleRotor.setInitialPosition('I');
var leftRotor = new enigma.RotorIV();
leftRotor.setInnerPosition('M');
leftRotor.setInitialPosition('Q');
machine.setRotors(leftRotor, middleRotor, rightRotor);
// Setting up the reflector
machine.setReflector(new enigma.ReflectorB());
// Encoding
var input = 'MYNAMEISMATHEUSPORTELAANDIAMBUILDINGANENIGMAMACHINESIMULATOR';
var output = '';
for (var i = 0; i < input.length; i++)
output += machine.encode(input[i]);
// Printing results
console.log(output)
Output:
VNZSOTTMKBFFFAOXRFGFEFGAHYNBFFYCNEMUBYUOQSHRWYROQODMCHTDHRWP