1.最好在winform中添加控件,保证效果。
控件事件代码(上面图片数字为文本控件和按钮控件的标示)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox5.Text.Trim().Length != 8)
{
MessageBox.Show(" KEY为8位字符", "加密提示");
this.textBox5.Focus();
return;
}
if (this.textBox1.Text.Trim().Length == 0)
{
MessageBox.Show(" 请输入密码", "输入提示");
this.textBox1.Focus();
return;
}
string pass = textBox1.Text;
textBox4.Text = textBox2.Text = DES.Encrypt(pass, textBox5.Text);
}
private void button2_Click(object sender, EventArgs e)
{
if (this.textBox5.Text != this.textBox6.Text)
{
MessageBox.Show("加密解密的KEY要保证一致!", "解密提示");
this.textBox6.Focus();
return;
}
if (this.textBox4.Text.Trim().Length == 0)
{
MessageBox.Show("加密码不能为空", "输入提示");
this.textBox1.Focus();
return;
}
string pass = textBox4.Text;
textBox3.Text = DES.Decrypt(pass, textBox6.Text);
}
private void button4_Click(object sender, EventArgs e)
{
textBox6.Text = textBox5.Text;
}
private void label7_Click(object sender, EventArgs e)
{
textBox6.Text = null;
}
}
}
2. 核心代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace WindowsFormsApplication8
{
public class DES
{
/// <summary>
/// 进行DES加密。
/// </summary>
/// <param name="pToEncrypt">要加密的字符串。</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>以Base64格式返回的加密字符串。</returns>
public static string Encrypt(string pToEncrypt, string sKey)
{
if (string.Empty.Equals(pToEncrypt))
return string.Empty;
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
return str;
}
}
/// <summary>
/// 进行DES解密。
/// </summary>
/// <param name="pToDecrypt">要解密的以Base64</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>已解密的字符串。</returns>
public static string Decrypt(string pToDecrypt, string sKey)
{
if (string.Empty.Equals(pToDecrypt))
return string.Empty;
byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
}
}