Exemple #1
0
    private void ButtonClick(object sender, EventArgs e)
    {
        if (ErrorsFound())
        {
            var errorMessage = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.None,
                                                 "Wrong pararameters\nOne rotor can not be used twice");
            errorMessage.ShowAll();
            return;
        }
        BuildRotors();
        var left   = AssignRotor(rotorLeftType.ActiveText);
        var center = AssignRotor(rotorCenterType.ActiveText);
        var right  = AssignRotor(rotorRightType.ActiveText);

        BuildReflectors();
        var reflector = AssignReflector(reflectorType.ActiveText);

        var plugboardCipher = BuildPlugboardCipher(GetPlugboardString());

        BuildPlugboard(plugboardCipher);

        var posRef      = reflectorPosition.Active;
        var startLeft   = rotorLeftStartPosition.Active;
        var startCenter = rotorCenterStartPosition.Active;
        var startRight  = rotorRightStartPosition.Active;

        var ringLeft   = rotorLeftRingPosition.Active;
        var ringCenter = rotorCenterRingPosition.Active;
        var ringRight  = rotorRightRingPosition.Active;

        var machine = new Enigma.Enigma(_plugboard, left, center, right, reflector);

        machine.SetRingPositions(posRef, ringLeft, ringCenter, ringRight);
        machine.SetPositions(startLeft, startCenter, startRight);

        var message        = inputText.Buffer.Text;
        var messageLetters = "";

        foreach (var ch in message)
        {
            if (char.IsUpper(ch))
            {
                messageLetters += ch;
            }
            else if (char.IsLower(ch))
            {
                messageLetters += char.ToUpper(ch);
            }
        }

        var res = messageLetters.Aggregate("", (current, t) => current + (char)(machine.Convert(t - 'A') + 'A'));

        outputText.Buffer.Text = res;
    }
Exemple #2
0
        /// <summary>
        /// Main function.
        /// </summary>
        /// <param name="args">Program arguments.</param>
        public static void Main(string[] args)
        {
            CheckForErrors(args);

            BuildRotors();
            var left   = AssignRotor(args[1]);
            var center = AssignRotor(args[2]);
            var right  = AssignRotor(args[3]);

            BuildReflectors();
            var reflector = AssignReflector(args[0]);

            if (args.Length == 7)
            {
                var plugboardCipher = BuildPlugboardCipher(args[6]);
                BuildPlugboard(plugboardCipher);
            }
            else
            {
                BuildPlugboard();
            }

            var posRef     = args[4][0] - 'A';
            var ringLeft   = args[4][1] - 'A';
            var ringCenter = args[4][2] - 'A';
            var ringRight  = args[4][3] - 'A';

            var startLeft   = args[5][0] - 'A';
            var startCenter = args[5][1] - 'A';
            var startRight  = args[5][2] - 'A';


            var machine = new Enigma(_plugboard, left, center, right, reflector);

            machine.SetRingPositions(posRef, ringLeft, ringCenter, ringRight);
            machine.SetPositions(startLeft, startCenter, startRight);

            var message        = Console.ReadLine();
            var messageLetters = "";

            if (message == null)
            {
                Console.WriteLine("Invalid input.");
                Environment.Exit(InvalidInput);
            }

            foreach (var ch in message)
            {
                if (char.IsUpper(ch))
                {
                    messageLetters += ch;
                }
                else if (char.IsLower(ch))
                {
                    messageLetters += char.ToUpper(ch);
                }
            }


            var res = messageLetters.Aggregate("", (current, t) => current + (char)(machine.Convert(t - 'A') + 'A'));

            Console.WriteLine(res);
        }