55 "EKMFLGDQVZNTOWYHXUSPAIBRCJ",
56 "AJDKSIRUXBLHWTMCQGZNPYFVOE",
57 "BDFHJLCPRTXVZNYEIWGAKMUSQO"
59 reflector:
string =
"YRUHQSLDPXNGOKMIEBFZCWVJAT"
63 this.reflector = reflector;
64 this.positions = Array(rotors.length).fill(0);
73 private step(): void {
74 this.positions[0] = this.mod(this.positions[0] + 1, 26);
76 for (let i = 0; i < this.positions.length - 1; i++) {
77 if (this.positions[i] === 0) {
78 this.positions[i + 1] = this.mod(this.positions[i + 1] + 1, 26);
91 encode(plainText:
string =
""):
string {
94 for (
const ch of plainText.toUpperCase()) {
95 if (!/[A-Z]/.test(ch)) {
106 for (let r = 0; r < this.rotors.length; r++) {
107 const rotor = this.rotors[r];
108 const shifted = this.mod(idx + this.positions[r], 26);
109 const mappedChar = rotor[shifted];
117 for (let r = this.rotors.length - 1; r >= 0; r--) {
118 const rotor = this.rotors[r];
119 const shifted = this.mod(idx + this.positions[r], 26);
121 idx = this.mod(pos - this.positions[r], 26);
138 decode(cipherText:
string =
""):
string {
140 return this.encode(cipherText);
constructor(rotors:string[]=["EKMFLGDQVZNTOWYHXUSPAIBRCJ", "AJDKSIRUXBLHWTMCQGZNPYFVOE", "BDFHJLCPRTXVZNYEIWGAKMUSQO"], reflector:string="YRUHQSLDPXNGOKMIEBFZCWVJAT")
Constructor for Enigma cipher.