/密码学/des/DES/windows2.cs

https://github.com/usecodelee/encryption-algorithm · C# · 134 lines · 109 code · 9 blank · 16 comment · 17 complexity · 0f045c6d90aeaf5a7758596258363b03 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Security.Cryptography;
  9. using System.IO;
  10. using System.Globalization;
  11. using System.Diagnostics;
  12. namespace des
  13. {
  14. public partial class windows2 : UserControl
  15. {
  16. //Stopwatch sw3 = new Stopwatch();
  17. Stopwatch sw4 = new Stopwatch();
  18. //int a, b;
  19. public windows2()
  20. {
  21. InitializeComponent();
  22. }
  23. private void windows2_Load(object sender, EventArgs e)
  24. {
  25. textBox6.Focus();
  26. }
  27. //快捷键
  28. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  29. {
  30. if (keyData == Keys.Enter)
  31. {
  32. this.button2.PerformClick();
  33. //this.btnTempTest.PerformClick();
  34. }
  35. if (keyData == (Keys.O | Keys.Alt))
  36. {
  37. this.button5.PerformClick();
  38. //this.btnTempTest.PerformClick();
  39. }
  40. if (keyData == (Keys.S | Keys.Alt))
  41. {
  42. this.button6.PerformClick();
  43. //this.btnTempTest.PerformClick();
  44. }
  45. return base.ProcessCmdKey(ref msg, keyData);
  46. }
  47. private void button5_Click(object sender, EventArgs e)
  48. {
  49. OpenFileDialog File3 = new OpenFileDialog();
  50. File3.Filter = "文本文件|*.txt";
  51. if (File3.ShowDialog() == DialogResult.OK)
  52. {
  53. // sw3.Start();
  54. StreamReader sr = File.OpenText(File3.FileName);
  55. while (sr.EndOfStream != true)
  56. this.textBox6.Text = (sr.ReadLine());
  57. this.textBox8.Text = File3.FileName;
  58. // sw3.Stop();
  59. }
  60. }
  61. private void button2_Click(object sender, EventArgs e)
  62. {
  63. if (textBox6.Text == "")
  64. {
  65. MessageBox.Show("请输入要解密的字符串!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  66. return;
  67. }
  68. if (textBox5.Text.Length != 8)
  69. {
  70. MessageBox.Show("请输入8位密钥!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  71. return;
  72. }
  73. textBox4.Text = DesDecrypt(textBox6.Text, textBox5.Text);
  74. }
  75. /// <summary>
  76. /// 解密原函数
  77. /// </summary>
  78. /// <param name="pToDecrypt"></param>
  79. /// <param name="sKey"></param>
  80. /// <returns></returns>
  81. public string DesDecrypt(string pToDecrypt, string sKey)
  82. {
  83. sw4.Start();
  84. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  85. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  86. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  87. {
  88. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  89. inputByteArray[x] = (byte)i;
  90. }
  91. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  92. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  93. MemoryStream ms = new MemoryStream();
  94. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  95. cs.Write(inputByteArray, 0, inputByteArray.Length);
  96. try
  97. {
  98. cs.FlushFinalBlock();
  99. }
  100. catch
  101. {
  102. return ("密钥或密文不正确!");
  103. }
  104. StringBuilder ret = new StringBuilder();
  105. sw4.Stop();
  106. TimeSpan ts3 = sw4.Elapsed;
  107. // a = int.Parse(sw3.ElapsedMilliseconds.ToString()); b = int.Parse(sw4.ElapsedMilliseconds.ToString());
  108. //MessageBox.Show("解密用时" +ts3 , "计时结果");
  109. label1.Text = ts3.ToString();
  110. return System.Text.Encoding.Default.GetString(ms.ToArray());
  111. }
  112. private void button6_Click(object sender, EventArgs e)
  113. {
  114. SaveFileDialog file4 = new SaveFileDialog();
  115. file4.Filter = "文本文件|*.txt";
  116. if (file4.ShowDialog() == DialogResult.OK)
  117. {
  118. StreamWriter sw = File.AppendText(file4.FileName);
  119. sw.Write(textBox4.Text);
  120. sw.Flush();
  121. sw.Close();
  122. }
  123. }
  124. }
  125. }