예제 #1
0
        static void Main(string[] args)
        {
            Rotor RotorA = new Rotor(0, "DMTWSILRUYQNKFEJCAZBPGXOHV");

            RotorA.Index = 15;

            Rotor RotorB = new Rotor(0, "HQZGPJTMOBLNCIFDYAWVEUSRKX");

            RotorB.Index = 13;

            Rotor RotorC = new Rotor(0, "UQNTLSZFMREHDPXKIBVYGJCWOA");

            RotorC.Index = 20;


            Switchboard SwitchboardA = new Switchboard("AJDKSIRUXBLHWTMCQGZNPYFVOE");
            Reflector   ReflectorA   = new Reflector("YRUHQSLDPXNGOKMIEBFZCWVJAT");

            EnigmaMachine Machine1 = new EnigmaMachine(SwitchboardA, new List <Rotor> {
                RotorA, RotorB, RotorC
            }, ReflectorA);

            ConsoleKeyInfo Key;

            do
            {
                Key = Console.ReadKey(true);
                if (Key.Key != ConsoleKey.Enter)
                {
                    Console.Title = String.Format("Rotor Position {1},{2},{3} : Received {0}", char.ToUpper(Key.KeyChar), RotorA.Index, RotorB.Index, RotorC.Index);
                    Console.Write((char)(Machine1.GetNextKey(char.ToUpper(Key.KeyChar) - 64) + 64));
                }
            } while (Key.Key != ConsoleKey.Enter);
        }
예제 #2
0
        static void Main(string[] args)
        {
            EnigmaMachine enigmaMachine = new EnigmaMachine(Rotor.VI, Rotor.I, Rotor.III, reflector: Reflector.ETW);

            // Plug in connections on the plug board
            enigmaMachine.PlugBoard.Plug('Z', 'T');
            enigmaMachine.PlugBoard.Plug('N', 'Y');
            enigmaMachine.PlugBoard.Plug('K', 'P');
            enigmaMachine.PlugBoard.Plug('A', 'Q');

            // set the Rotor Positions
            enigmaMachine.Rotors[0].SetPosition('K');
            enigmaMachine.Rotors[1].SetPosition('F');
            enigmaMachine.Rotors[2].SetPosition('C');

            // set the ring settings (changes the point where rotor rolls over the next rotor.
            enigmaMachine.Rotors[0].RingSetting = 20;
            enigmaMachine.Rotors[1].RingSetting = 2;
            enigmaMachine.Rotors[2].RingSetting = 11;

            Console.WriteLine(enigmaMachine);

            Console.Write("Enter a Message:  ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            string message = Console.ReadLine();

            Console.ForegroundColor = ConsoleColor.White;

            string encrypted = enigmaMachine.Process(message);

            Console.Write("Encrypted:  ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(encrypted);
            Console.ForegroundColor = ConsoleColor.White;
            Console.ReadLine();

            // Emulate a second machine with the same settings
            EnigmaMachine enigmaMachine2 = new EnigmaMachine(Rotor.VIII, Rotor.VI, Rotor.II, reflector: Reflector.ETW);

            enigmaMachine2.PlugBoard.Plug('F', 'T');
            enigmaMachine2.PlugBoard.Plug('N', 'Y');
            enigmaMachine2.PlugBoard.Plug('G', 'P');
            enigmaMachine2.PlugBoard.Plug('A', 'Q');

            enigmaMachine2.Rotors[0].SetPosition('K');
            enigmaMachine2.Rotors[1].SetPosition('F');
            enigmaMachine2.Rotors[2].SetPosition('C');
            enigmaMachine2.Rotors[0].RingSetting = 22;
            enigmaMachine2.Rotors[1].RingSetting = 2;
            enigmaMachine2.Rotors[2].RingSetting = 11;

            string output = enigmaMachine2.Process(encrypted);

            Console.Write("Decrypted:  ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(output);
            Console.ReadLine();
        }
예제 #3
0
        static void Main(string[] args)
        {
            EnigmaMachine enigmaMachine = new EnigmaMachine(Rotor.VIII, Rotor.VI, Rotor.II, reflector: Reflector.ETW);

            enigmaMachine.PlugBoard.Plug('A', 'T');
            enigmaMachine.PlugBoard.Plug('E', 'Y');
            enigmaMachine.PlugBoard.Plug('K', 'O');
            enigmaMachine.PlugBoard.Plug('N', 'P');

            enigmaMachine.Rotors[0].SetPosition('K');
            enigmaMachine.Rotors[1].SetPosition('F');
            enigmaMachine.Rotors[2].SetPosition('C');
            enigmaMachine.Rotors[0].RingSetting = 20;
            enigmaMachine.Rotors[1].RingSetting = 2;
            enigmaMachine.Rotors[2].RingSetting = 11;

            Console.WriteLine(enigmaMachine);

            Console.Write("Enter a Message:  ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            string message = Console.ReadLine();

            Console.ResetColor();

            string encrypted = enigmaMachine.Process(message);

            Console.Write("Encrypted:  ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(encrypted);
            Console.ResetColor();
            Console.ReadLine();

            // Emulate a second machine with the same settings
            EnigmaMachine enigmaMachine2 =
                new EnigmaMachine(Rotor.VIII, Rotor.VI, Rotor.II, reflector: Reflector.ETW);

            enigmaMachine2.PlugBoard.Plug('A', 'T');
            enigmaMachine2.PlugBoard.Plug('E', 'Y');
            enigmaMachine2.PlugBoard.Plug('K', 'O');
            enigmaMachine2.PlugBoard.Plug('N', 'P');

            enigmaMachine2.Rotors[0].SetPosition('K');
            enigmaMachine2.Rotors[1].SetPosition('F');
            enigmaMachine2.Rotors[2].SetPosition('C');
            enigmaMachine2.Rotors[0].RingSetting = 20;
            enigmaMachine2.Rotors[1].RingSetting = 2;
            enigmaMachine2.Rotors[2].RingSetting = 11;

            string output = enigmaMachine2.Process(encrypted);

            Console.Write("Decrypted:  ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(output);
            Console.ResetColor();
            Console.ReadLine();
        }
예제 #4
0
        private void CheckConfiguration()
        {
            if ((CbxRotor1.Text.Length != 0) && (CbxRotor2.Text.Length != 0))
            {
                if ((CbxRotor1.Text == CbxRotor2.Text))
                {
                    MessageBox.Show("Rotors 1 and 2 cannot match!");
                }
            }

            if ((CbxRotor2.Text.Length != 0) && (CbxRotor3.Text.Length != 0))
            {
                if ((CbxRotor2.Text == CbxRotor3.Text))
                {
                    MessageBox.Show("Rotors 2 and 3 cannot match!");
                }
            }

            if ((CbxRotor1.Text.Length != 0) && (CbxRotor3.Text.Length != 0))
            {
                if (CbxRotor1.Text == CbxRotor3.Text)
                {
                    MessageBox.Show("Rotors 1 and 3 cannot match!");
                }
            }

            if ((CbxRotor1.Text.Length != 0) && (CbxRotor2.Text.Length != 0) && (CbxRotor3.Text.Length != 0))
            {
                if ((CbxStartPos1.Text.Length != 0) && (CbxStartPos2.Text.Length != 0) && (CbxStartPos3.Text.Length != 0))
                {
                    RotorName rotor1 = CreateRotor(CbxRotor1.Text);
                    RotorName rotor2 = CreateRotor(CbxRotor2.Text);
                    RotorName rotor3 = CreateRotor(CbxRotor3.Text);

                    int startPos1 = int.Parse(CbxStartPos1.Text);
                    int startPos2 = int.Parse(CbxStartPos2.Text);
                    int startPos3 = int.Parse(CbxStartPos3.Text);

                    _enigmaMachine = new EnigmaMachine(new[] { rotor1, rotor2, rotor3 }, new[] { startPos1, startPos2, startPos3 });

                    EnablePlugs();
                    BtnConfigure.Enabled = true;
                    return;
                }
            }

            RtbContent.Enabled = false;
            RtbOutput.Enabled  = false;
            DisablePlugs();
            BtnConfigure.Enabled = false;
            BtnEncode.Enabled    = false;
        }
예제 #5
0
        static void Main(string[] args)
        {
            EnigmaMachine  machine   = new EnigmaMachine();
            EnigmaSettings eSettings = new EnigmaSettings();

            querySettings(eSettings);

            string message = "";

            Console.Write("Enter message to encrypt: ");
            message = Console.ReadLine();
            while (!Regex.IsMatch(message, @"^[a-zA-Z ]+$"))
            {
                Console.Write("Only letters A-Z is allowed, try again: ");
                message = Console.ReadLine();
            }
            message = message.Replace(" ", "").ToUpper();

            // Enter settings on machine
            machine.setSettings(eSettings.rings, eSettings.grund, eSettings.order, eSettings.reflector);

            // The plugboard settings
            foreach (string plug in eSettings.plugs)
            {
                char[] p = plug.ToCharArray();
                machine.addPlug(p[0], p[1]);
            }

            // Message encrypt
            Console.WriteLine();
            Console.WriteLine("Plain text:\t" + message);
            string enc = machine.runEnigma(message);

            Console.WriteLine("Encrypted:\t" + enc);

            // Reset the settings before decrypting!
            machine.setSettings(eSettings.rings, eSettings.grund, eSettings.order, eSettings.reflector);

            // Message decrypt
            string dec = machine.runEnigma(enc);

            Console.WriteLine("Decrypted:\t" + dec);
            Console.WriteLine();

            Console.ReadLine();
        }