///<inheritdoc/> public override string Run(string input, bool forward) { var keytable = new Alphabet(Alphabet.ToString()) { Dimensions = Alphabet.Dimensions }; var distinct = Key.Distinct().ToList(); for (int i = 0; i < distinct.Count; i++) { keytable.Move(distinct[i], i); } if (input.Length % 2 != 0) { input += Alphabet[Alphabet.Count - 1]; } var multiplier = forward ? 1 : -1; var output = new StringBuilder(); //Let the ciphering begin: for (int i = 0; i < input.Length; i += 2) { var a = keytable.DimensionsOf(input[i]); var b = keytable.DimensionsOf(input[i + 1]); var newA = new Dimensions(b.X, a.Y); var newB = new Dimensions(a.X, b.Y); if (a.X == b.X || a.Y == b.Y) { newA = a; newB = b; //SamePoint if (a == b) { newA.X = PrefferredOrientation == PrefferredOrientation.Horizontal ? newA.X += multiplier : newA.X; newA.Y = PrefferredOrientation == PrefferredOrientation.Vertical ? newA.Y += multiplier : newA.Y; newB = new Dimensions(newA.X, newA.Y); } //SameCol else if (a.X == b.X) { newA.Y += multiplier; newB.Y += multiplier; } //SameRow else { newA.X += multiplier; newB.X += multiplier; } newA.Limit(keytable.Dimensions); newB.Limit(keytable.Dimensions); } output.Append(keytable[newA]); output.Append(keytable[newB]); } return(output.ToString()); }
public void Save(Stream stream) { //header using var writer = new BinaryWriter(stream); //Header writer.Write("EGMC"); //Alphabet writer.Write("alph"); writer.Write(Alphabet.Count); writer.Write(Alphabet.ToString()); //Whether we autoreset writer.Write(AutoReset); //Stator Indexing: writer.Write("ETW "); writer.Write("indx"); writer.Write(Stator.Indexing.Count); writer.Write(Stator.Indexing.ToString()); //Stator Wiring writer.Write("wire"); writer.Write(Stator.Wiring.Count); writer.Write(Stator.Wiring.ToString()); //Reflector Indexing writer.Write("UKW "); writer.Write("indx"); writer.Write(Reflector.Indexing.Count); writer.Write(Reflector.Indexing.ToString()); //Reflector Wiring writer.Write("wire"); writer.Write(Reflector.Wiring.Count); writer.Write(Reflector.Wiring.ToString()); //Rotors writer.Write("ROTS"); writer.Write(Rotors.Count); foreach (var rotor in Rotors) { //Indexing writer.Write("indx"); writer.Write(rotor.Indexing.Count); writer.Write(rotor.Indexing.ToString()); //Wiring writer.Write("wire"); writer.Write(rotor.Wiring.Count); writer.Write(rotor.Wiring.ToString()); //Turnover Notch writer.Write("ntch"); writer.Write(rotor.TurnOver.Count); foreach (var notch in rotor.TurnOver) { writer.Write(notch); } } }
/// <summary> /// Builds an <see cref="EnigmaMachine"/> with the provided parameters. /// </summary> /// <returns></returns> public EnigmaMachine Build() { var shuffle = new Alphabet(_alphabet.ToString()); //the stator: var stator = _stator; if (stator == null) { stator = new EnigmaWheel(_alphabet.ToString(), _alphabet.ToString()); } //the reflector var reflector = _reflector; if (reflector == null) { shuffle.Shuffle(); reflector = new EnigmaWheel(_alphabet.ToString(), shuffle.ToString()); reflector.Reflect(); } //the rotors List <Rotor> rotors; if (_rotors == null) { rotors = new List <Rotor>(); for (int i = 0; i < _rotorCount; i++) { string notches = ""; for (int n = 0; n < _notchCount; n++) { char c; do { int rnd = Horus.Random(0, _alphabet.Count); c = _alphabet[rnd]; } while (notches.Contains(c)); notches += c; } shuffle.Shuffle(); var rotor = new Rotor(_alphabet.ToString(), shuffle.ToString(), notches); rotors.Add(rotor); } } else { rotors = new List <Rotor>(_rotors); } //the actual construction return(new EnigmaMachine(rotors) { Alphabet = _alphabet, AutoReset = _autoReset, Stator = stator, Reflector = reflector, }); }