コード例 #1
0
        public EnigmaMachine(PlugBoard plugBoard)
        {
            var rotor  = new Rotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", 'Q');
            var rotor2 = new Rotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", 'E');
            var rotor3 = new Rotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", 'V');

            rotors = new[] { rotor, rotor2, rotor3 };
            foreach (var rtr in rotors)
            {
                rtr.OnRotated += (sender, ch) =>
                {
                    OnRotorRotated(Array.IndexOf(rotors, sender), ch);
                };
            }
            reflector      = new Rotor("EJMZALYXVBWFCRQUONTSPIKHGD", 'D');
            this.plugBoard = plugBoard;
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: JoYeRsOl/EnigmaEmulator
        private void Form1_Load(object sender, EventArgs e)
        {
            Label[]  labels = { Lamp_Q, Lamp_W, Lamp_E, Lamp_R, Lamp_T, Lamp_Y, Lamp_U, Lamp_I, Lamp_O, Lamp_P,
                                Lamp_A,  Lamp_S, Lamp_D, Lamp_F, Lamp_G, Lamp_H, Lamp_J, Lamp_K, Lamp_L,
                                Lamp_Z,  Lamp_X, Lamp_C, Lamp_V, Lamp_B, Lamp_N, Lamp_M };
            Button[] buttons = { Button_Q, Button_W, Button_E, Button_R, Button_T, Button_Y, Button_U, Button_I, Button_O, Button_P,
                                 Button_A, Button_S, Button_D, Button_F, Button_G, Button_H, Button_J, Button_K, Button_L,
                                 Button_Z, Button_X, Button_C, Button_V, Button_B, Button_N, Button_M };
            Label[]  Plugs = { Plug_Q, Plug_W, Plug_E, Plug_R, Plug_T, Plug_Y, Plug_U, Plug_I, Plug_O, Plug_P,
                               Plug_A,  Plug_S, Plug_D, Plug_F, Plug_G, Plug_H, Plug_J, Plug_K, Plug_L,
                               Plug_Z,  Plug_X, Plug_C, Plug_V, Plug_B, Plug_N, Plug_M };

            rotorsSettings = new List <RotorSettings>()
            {
                new RotorSettings("EKMFLGDQVZNTOWYHXUSPAIBRCJ", 'Q', 0), new RotorSettings("AJDKSIRUXBLHWTMCQGZNPYFVOE", 'E', 0),
                new RotorSettings("BDFHJLCPRTXVZNYEIWGAKMUSQO", 'V', 0), new RotorSettings("ESOVPZJAYQUIRHXLNFTGKDCMWB", 'J', 0),
                new RotorSettings("VZBRGITYUPSDNHLXAWMJQOFECK", 'Z', 0), new RotorSettings("JPGVOUMFYQBENHZRDKASXLICTW", 'Z', 0),
                new RotorSettings("NZJHGRCXMYSWBOUFAIVLPEKQDT", 'Z', 0), new RotorSettings("FKQHTLXOCBJSPDZRAMEWNIUYGV", 'Z', 0)
            };

            reflectors = new List <RotorSettings>()
            {
                new RotorSettings("EJMZALYXVBWFCRQUONTSPIKHGD", 'A', 0), //A
                new RotorSettings("YRUHQSLDPXNGOKMIEBFZCWVJAT", 'A', 0), //B
                new RotorSettings("FVPJIAOYEDRZXWGCTKUQSBNMHL", 'A', 0), //C
                new RotorSettings("ENKQAUYWJICOPBLMDXZVFTHRGS", 'A', 0)  //B Thin
            };

            lampBoard = new LampBoard(labels);
            keyBoard  = new KeyBoard(buttons);
            plugBoard = new PlugBoard(Plugs);
            wheels    = new int[3];

            var rotorI   = new Rotor(rotorsSettings[0]);
            var rotorII  = new Rotor(rotorsSettings[1]);
            var rotorIII = new Rotor(rotorsSettings[2]);

            machine = new EnigmaMachine(plugBoard);

            machine.OnRotorRotated += (i, ch) =>
            {
                switch (i)
                {
                case 0:
                    r1.Text = ch.ToString();
                    break;

                case 1:
                    r2.Text = ch.ToString();
                    break;

                case 2:
                    r3.Text = ch.ToString();
                    break;
                }
            };
            machine.Refresh();

            r1.MouseWheel += textBoxR_Wheel;
            r2.MouseWheel += textBoxR_Wheel;
            r3.MouseWheel += textBoxR_Wheel;


            keyBoard.OnKeyDown += (c) => { var ch = machine.Encrypt(c); boxOut.Text += ch; boxIn.Text += c; lampBoard.Enable(ch); };
            keyBoard.OnKeyUp   += (c) => { lampBoard.Disable(); };

            machineSettings = new EnigmaSettings(machine.GetSettings());
            settingsToReset = new EnigmaSettings(machine.GetSettings());

            boxIn.KeyDown              += KeyDown_Delete_Back;
            boxOut.KeyDown             += KeyDown_Delete_Back;
            selector.OnSelectorChanged += (i) => {
                Console.WriteLine(i);
                machine.SetReflector(reflectors[i]);
            };
        }