示例#1
0
        static void TestRotors()
        {
            Rotor rotorIII = Rotor.III;
            Rotor rotorVI  = Rotor.VI;
            Rotor rotorI   = Rotor.I;

            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char ech = rotorIII.In(ch);
                char och = rotorIII.Out(ech);
                Console.WriteLine("{0} => {1} => {2}", ch, ech, och);
            }
            Console.WriteLine("________________________________");
            Console.ReadLine();

            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char ech = rotorVI.In(ch);
                char och = rotorVI.Out(ech);
                Console.WriteLine("{0} => {1} => {2}", ch, ech, och);
            }
            Console.WriteLine("________________________________");
            Console.ReadLine();

            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char ech = rotorI.In(ch);
                char och = rotorI.Out(ech);
                Console.WriteLine("{0} => {1} => {2}", ch, ech, och);
            }
            Console.WriteLine("________________________________");
            Console.ReadLine();
            Reflector reflector = Reflector.A;

            rotorIII.Increment();
            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char c1 = rotorIII.In(ch);
                char c2 = rotorVI.In(c1);
                char c3 = rotorI.In(c2);
                char r  = reflector.Reflect(c3);
                char c4 = rotorI.Out(r);
                char c5 = rotorVI.Out(c4);
                char c6 = rotorIII.Out(c5);
                Console.WriteLine("{0} => {1} => {2} => {3} => {4} => {5} => {6} => {7}", ch, c1, c2, c3, r, c4, c5, c6);

                char dc1 = rotorIII.In(c6);
                char dc2 = rotorVI.In(dc1);
                char dc3 = rotorI.In(dc2);
                char dr  = reflector.Reflect(dc3);
                char dc4 = rotorI.Out(dr);
                char dc5 = rotorVI.Out(dc4);
                char dc6 = rotorIII.Out(dc5);
                Console.WriteLine("{7} <= {6} <= {5} <= {4} <= {3} <= {2} <= {1} <= {0}", c6, dc1, dc2, dc3, dr, dc4, dc5, dc6);
                Console.WriteLine("______________________________________________________________________________________");
                Console.ReadLine();
            }
        }
示例#2
0
        static void TestRotors()
        {
            Rotor rotorIII = Rotor.III;
            Rotor rotorVI  = Rotor.VI;
            Rotor rotorI   = Rotor.I;

            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char ech = rotorIII.In(ch);
                char och = rotorIII.Out(ech);
                Console.WriteLine("{0} => {1} => {2}", ch, ech, och);
            }
            Console.WriteLine("________________________________");
            Console.ReadLine();

            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char ech = rotorVI.In(ch);
                char och = rotorVI.Out(ech);
                Console.WriteLine($"{ch} => {ech} => {och}");
            }
            Console.WriteLine("________________________________");
            Console.ReadLine();

            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char ech = rotorI.In(ch);
                char och = rotorI.Out(ech);
                Console.WriteLine($"{ch} => {ech} => {och}");
            }
            Console.WriteLine("________________________________");
            Console.ReadLine();
            Reflector reflector = Reflector.A;

            rotorIII.Increment();
            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                char c1 = rotorIII.In(ch);
                char c2 = rotorVI.In(c1);
                char c3 = rotorI.In(c2);
                char r  = reflector.Reflect(c3);
                char c4 = rotorI.Out(r);
                char c5 = rotorVI.Out(c4);
                char c6 = rotorIII.Out(c5);
                Console.WriteLine($"{ch} => {c1} => {c2} => {c3} => {r} => {c4} => {c5} => {c6}");

                char dc1 = rotorIII.In(c6);
                char dc2 = rotorVI.In(dc1);
                char dc3 = rotorI.In(dc2);
                char dr  = reflector.Reflect(dc3);
                char dc4 = rotorI.Out(dr);
                char dc5 = rotorVI.Out(dc4);
                char dc6 = rotorIII.Out(dc5);
                Console.WriteLine($"{dc6} <= {dc5} <= {dc4} <= {dr} <= {dc3} <= {dc2} <= {dc1} <= {c6}");
                Console.WriteLine("______________________________________________________________________________________");
                Console.ReadLine();
            }
        }
示例#3
0
        /// <summary>
        /// Emulate a key press on the Enigma Machine
        /// </summary>
        /// <param name="ch"></param>
        /// <returns>Encrypted character</returns>
        public char Process(char ch)
        {
            if (!CHARACTERS.Contains(ch))
            {
                return(ch);
            }

            //Console.Write("{0} => ", ch);
            // first step runs through the plugboard
            ch = PlugBoard[ch];

            //Console.Write("{0} => ", ch);
            // go IN through each rotor
            for (int i = 0; i < Rotors.Count; i++)
            {
                ch = Rotors[i].In(ch);

                //Console.Write("{0} => ", ch);
            }

            // feed through the reflector.
            ch = Reflector.Reflect(ch);

            //Console.Write("{0} => ", ch);
            // go OUT through each rotor in reverse order
            for (int i = Rotors.Count - 1; i >= 0; i--)
            {
                ch = Rotors[i].Out(ch);
                //Console.Write("{0} => ", ch);
            }

            // back out through the plugboard again
            ch = PlugBoard[ch];
            //Console.WriteLine("{0}", ch);

            for (int i = 0; i < Rotors.Count; i++)
            {
                if (!Rotors[i].Increment())
                {
                    break;
                }
            }

            return(ch);
        }
示例#4
0
        public string Transform(
            int[] wheelOrientations,
            string cipherText)
        {
            HandleTransformErrors(wheelOrientations);

            var currentlyRotatingWheel = 0;

            Reflector     reflector   = new Reflector();
            StringBuilder codeBuilder = new StringBuilder();

            foreach (char cipherLetter in cipherText)
            {
                if (cipherLetter == ' ')
                {
                    codeBuilder.Append(cipherLetter);
                    continue;
                }

                char wheelZeroEncrypt = CryptCharacter(0, wheelOrientations[0], cipherLetter, CryptType.Encrypt);
                char wheelOneEncrypt  = CryptCharacter(1, wheelOrientations[1], wheelZeroEncrypt, CryptType.Encrypt);
                char wheelTwoEncrypt  = CryptCharacter(2, wheelOrientations[2], wheelOneEncrypt, CryptType.Encrypt);

                char reflectedResult = reflector.Reflect(wheelTwoEncrypt);

                char wheelTwoDecrypt  = CryptCharacter(2, wheelOrientations[2], reflectedResult, CryptType.Decrypt);
                char wheelOneDecrypt  = CryptCharacter(1, wheelOrientations[1], wheelTwoDecrypt, CryptType.Decrypt);
                char wheelZeroDecrypt = CryptCharacter(0, wheelOrientations[0], wheelOneDecrypt, CryptType.Decrypt);

                codeBuilder.Append(wheelZeroDecrypt);

                wheelOrientations      = RotateCurrentWheel(wheelOrientations, currentlyRotatingWheel);
                currentlyRotatingWheel = SetCurrentlyRotatingWheel(wheelOrientations, currentlyRotatingWheel);
            }

            return(codeBuilder.ToString());
        }