コード例 #1
0
        /// <summary>
        /// 解密按钮点击后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            // 如果加密算法初始化失败
            if (!myCipherKeys.AllKeysReady)
            {
                MessageBox.Show("请检查密钥是否符合要求",
                                "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            myCipher = new MyCipher(myCipherKeys);

            // 判断源文本是否符合要求
            if (!isSourceStringOk)
            {
                MessageBox.Show("待处理文本必须是26个字母",
                                "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!isSourceStringOk || "".Equals(tbSource.Text))
            {
                MessageBox.Show("待处理文本为空,请输入内容",
                                "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (ckbPasteFromClipBoard.Checked)
            {
                if (!string.Empty.Equals(Clipboard.GetText()))
                {
                    try
                    {
                        tbSource.Text = Clipboard.GetText();
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show(exp.Message,
                                        "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            try
            {
                tbTarget.Text = myCipher.Decrypt(tbSource.Text);
            }
            catch (Exception exc)
            {
                MessageBox.Show($"解密信息的过程中出错了,错误信息:{exc.Message}",
                                "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #2
0
        public void MyCipherTest()
        {
            int time = 100;

            int[] vigenereKey;
            int   affineKeyA;
            int   affineKeyB;

            int[,] hillMatrix;

            for (int i = 0; i < time; i++)
            {
                GetRandomKeys(out vigenereKey, out affineKeyA, out affineKeyB, out hillMatrix);
                var mycipher = new MyCipher(vigenereKey, affineKeyA, affineKeyB, hillMatrix);
                var plain    = RandomHelper.GetString();
                Assert.AreEqual(plain, mycipher.Decrypt(mycipher.Encrypt(plain)));
            }
        }