/// <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); } }
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))); } }
/// <summary> /// 加密按钮点击后 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnEncrypt_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; } // 将明文转成全小写 this.tbSource.Text = this.tbSource.Text.ToLower(); try { tbTarget.Text = myCipher.Encrypt(tbSource.Text); } catch (Exception exc) { MessageBox.Show($"加密信息的过程中出错了,错误信息:{exc.Message}", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (ckbCopyToClipBoard.Checked) { Clipboard.SetDataObject(tbTarget.Text); } }