Beispiel #1
0
        private async void decrypt_btn_Click(object sender, EventArgs e)
        {
            if (Is_Decrypting)
            {
                if (MessageBox.Show(icon: MessageBoxIcon.Warning, buttons: MessageBoxButtons.YesNo, caption: "Abort", text: "The program is decrypting now, are you sure you want to abort?") == DialogResult.No)
                {
                    return;
                }
                cancelTokenSource.Cancel();
                Is_Decrypting = false;
                //复苏被冻结的Textbox
                decrypt_btn.Text = "Decrypt";
                foreach (TextBox textBox in Plgbd.Controls)
                {
                    textBox.ReadOnly = false;
                }
                foreach (TextBox textBox in Rotor.Controls)
                {
                    textBox.ReadOnly = false;
                }
                foreach (TextBox textBox in RingSetting.Controls)
                {
                    textBox.ReadOnly = false;
                }
                foreach (TextBox textBox in MessageKey.Controls)
                {
                    textBox.ReadOnly = false;
                }
                CypherText.ReadOnly = MD5.ReadOnly = false;
                PlainText.ForeColor = Color.DarkBlue;
                PlainText.Text      = "**DECRYPT_ABORTED**";
                return;
            }

            Is_Decrypting = true;

            string decrypted = string.Empty; //解密后的明文
            int    count     = 0;            //缺失的信息的个数

            foreach (TextBox textBox in Rotor.Controls)
            {
                if (textBox.Text == string.Empty)
                {
                    count++;
                }
            }
            foreach (TextBox textBox in RingSetting.Controls)
            {
                if (textBox.Text == string.Empty)
                {
                    count++;
                }
            }
            foreach (TextBox textBox in MessageKey.Controls)
            {
                if (textBox.Text == string.Empty)
                {
                    count++;
                }
            }
            if (count == 6 && MessageBox.Show(icon: MessageBoxIcon.Warning, buttons: MessageBoxButtons.OKCancel, caption: "Warning", text: "This might take a long time, continue?") == DialogResult.Cancel)
            {
                return;
            }
            if (count >= 7 && MessageBox.Show(icon: MessageBoxIcon.Warning, buttons: MessageBoxButtons.OKCancel, caption: "Warning", text: "This might take VERY LONG time, continue?") == DialogResult.Cancel)
            {
                return;
            }

            //生成cancellationtoken与stopwatch
            CancellationToken cancellation = cancelTokenSource.Token;
            Stopwatch         stopwatch    = new Stopwatch();

            stopwatch.Start();
            //开始在UI线程上做相关的准备,比如更改button名字、Unable各种东西
            foreach (TextBox textBox in Plgbd.Controls)
            {
                textBox.ReadOnly = true;
            }
            foreach (TextBox textBox in Rotor.Controls)
            {
                textBox.ReadOnly = true;
            }
            foreach (TextBox textBox in RingSetting.Controls)
            {
                textBox.ReadOnly = true;
            }
            foreach (TextBox textBox in MessageKey.Controls)
            {
                textBox.ReadOnly = true;
            }
            CypherText.ReadOnly = MD5.ReadOnly = true;
            decrypt_btn.Text    = "Abort";
            PlainText.Text      = "Decrypting, Please wait";

            //开始运行破解任务
            await Task.Run(() =>
            {
                try
                {
                    var plugboard    = Get_Plugboard();
                    var rotor_num    = Get_Rotor();
                    var ring_setting = Get_Ring_Setting();
                    var message_key  = Get_Message_Key();
                    // plain_text = PlainText.Text;
                    string cypher_text = CypherText.Text;
                    string md5         = MD5.Text;
                    Enigma enigma      = new Enigma(plugboard, rotor_num, ring_setting, message_key, "", cypher_text, md5);
                    decrypted          = enigma.Decrpyt();
                    //显示破译数据
                    this.Invoke(new Action(() =>
                    {
                        if (Rotor1.Text == string.Empty || Rotor1.Text != enigma.rotor_num[0].ToString())
                        {
                            Rotor1.ForeColor = Color.DarkBlue; Rotor1.Text = enigma.rotor_num[0].ToString();
                        }
                        if (Rotor2.Text == string.Empty || Rotor2.Text != enigma.rotor_num[1].ToString())
                        {
                            Rotor2.ForeColor = Color.DarkBlue; Rotor2.Text = enigma.rotor_num[1].ToString();
                        }
                        if (Rotor3.Text == string.Empty || Rotor3.Text != enigma.rotor_num[2].ToString())
                        {
                            Rotor3.ForeColor = Color.DarkBlue; Rotor3.Text = enigma.rotor_num[2].ToString();
                        }
                        if (RS1.Text == string.Empty || RS1.Text != enigma.ring_setting[0].ToString())
                        {
                            RS1.ForeColor = Color.DarkBlue; RS1.Text = enigma.ring_setting[0].ToString();
                        }
                        if (RS2.Text == string.Empty || RS2.Text != enigma.ring_setting[1].ToString())
                        {
                            RS2.ForeColor = Color.DarkBlue; RS2.Text = enigma.ring_setting[1].ToString();
                        }
                        if (RS3.Text == string.Empty || RS3.Text != enigma.ring_setting[2].ToString())
                        {
                            RS3.ForeColor = Color.DarkBlue; RS3.Text = enigma.ring_setting[2].ToString();
                        }
                        if (MK1.Text == string.Empty || MK1.Text != enigma.message_key[0].ToString())
                        {
                            MK1.ForeColor = Color.DarkBlue; MK1.Text = enigma.message_key[0].ToString();
                        }
                        if (MK2.Text == string.Empty || MK2.Text != enigma.message_key[1].ToString())
                        {
                            MK2.ForeColor = Color.DarkBlue; MK2.Text = enigma.message_key[1].ToString();
                        }
                        if (MK3.Text == string.Empty || MK3.Text != enigma.message_key[2].ToString())
                        {
                            MK3.ForeColor = Color.DarkBlue; MK3.Text = enigma.message_key[2].ToString();
                        }
                    }));
                    stopwatch.Stop();
                    MessageBox.Show(text: "Success. Time:" + stopwatch.ElapsedMilliseconds / 1000.0 + "second", icon: MessageBoxIcon.Information, caption: "Success", buttons: MessageBoxButtons.OK);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(text: "Failed. Error:" + ex.Message + ", Time: " + stopwatch.ElapsedMilliseconds / 1000.0 + "second", icon: MessageBoxIcon.Error, caption: "Error", buttons: MessageBoxButtons.OK);
                }
            }, cancellation);

            Is_Decrypting = false;
            //复苏被冻结的Textbox
            decrypt_btn.Text = "Decrypt";
            foreach (TextBox textBox in Plgbd.Controls)
            {
                textBox.ReadOnly = false;
            }
            foreach (TextBox textBox in Rotor.Controls)
            {
                textBox.ReadOnly = false;
            }
            foreach (TextBox textBox in RingSetting.Controls)
            {
                textBox.ReadOnly = false;
            }
            foreach (TextBox textBox in MessageKey.Controls)
            {
                textBox.ReadOnly = false;
            }
            CypherText.ReadOnly = MD5.ReadOnly = false;
            PlainText.ForeColor = Color.DarkBlue;
            PlainText.Text      = decrypted;
        }