using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace MyDes
{
/// <summary>
/// DES加/解密与MD5加密
/// </summary>
public class My_Des
{
#region 私有属性
/// <summary>
/// MD5加密的字符串
/// </summary>
private string md5Str = null;
/// <summary>
/// DES加密的字符串
/// </summary>
private string encryptStr = null;
/// <summary>
/// DES解密的字符串
/// </summary>
private string decryptStr = null;
/// <summary>
/// DES密匙
/// </summary>
private string mydesKey = null;
/// <summary>
/// 返回的字符串
/// </summary>
private string mydesStr = null;
/// <summary>
/// 错误信息
/// </summary>
private string messAge = null;
#endregion
#region 公共属性
/// <summary>
/// MD5加密字符串
/// </summary>
public string MD5Str
{
get { return md5Str; }
set { md5Str = value; }
}
/// <summary>
/// DES加密的字符串
/// </summary>
public string EncryptStr
{
get { return encryptStr; }
set { encryptStr = value; }
}
/// <summary>
///DES 解密的字符串
/// </summary>
public string DecryptStr
{
get { return decryptStr; }
set { decryptStr = value; }
}
/// <summary>
/// DES密匙
/// </summary>
public string MyDesKey
{
get { return mydesKey; }
set { mydesKey = value; }
}
/// <summary>
/// 返回的字符串
/// </summary>
public string MyDesStr
{
get { return mydesStr; }
set { mydesStr = value; }
}
/// <summary>
/// 错误信息
/// </summary>
public string Message
{
get { return messAge; }
set { messAge = value; }
}
#endregion
#region 公共方法
/// <summary>
/// 执行DES加密
/// </summary>
public void DesEncrypt()
{
try
{
byte[] MyStr_E = Encoding.UTF8.GetBytes(this.encryptStr);
byte[] MyKey_E = Encoding.UTF8.GetBytes(this.mydesKey);
DESCryptoServiceProvider MyDes_E = new DESCryptoServiceProvider();
MyDes_E.Key = MyKey_E;
MyDes_E.IV = MyKey_E;
MemoryStream MyMem_E = new MemoryStream();
CryptoStream MyCry_E = new CryptoStream(MyMem_E, MyDes_E.CreateEncryptor(), CryptoStreamMode.Write);
MyCry_E.Write(MyStr_E, 0, MyStr_E.Length);
MyCry_E.FlushFinalBlock();
MyCry_E.Close();
this.mydesStr = Convert.ToBase64String(MyMem_E.ToArray());
}
catch (Exception Error)
{
this.messAge = "DES加密出错:"+Error.Message;
}
}
/// <summary>
/// 执行DES解密
/// </summary>
public void DesDecrypt()
{
try
{
byte[] MyStr_D = Convert.FromBase64String(this.decryptStr);
byte[] MyKey_D = Encoding.UTF8.GetBytes(this.mydesKey);
DESCryptoServiceProvider MyDes_D = new DESCryptoServiceProvider();
MyDes_D.Key = MyKey_D;
MyDes_D.IV = MyKey_D;
MemoryStream MyMem_D = new MemoryStream();
CryptoStream MyCry_D = new CryptoStream(MyMem_D, MyDes_D.CreateDecryptor(), CryptoStreamMode.Write);
MyCry_D.Write(MyStr_D, 0, MyStr_D.Length);
MyCry_D.FlushFinalBlock();
MyCry_D.Close();
this.mydesStr = Encoding.UTF8.GetString(MyMem_D.ToArray());
}
catch (Exception Error)
{
this.messAge ="DES解密出错:" +Error.Message;
}
}
/// <summary>
/// 执行MD5加密
/// </summary>
public void MD5JiaMi()
{
MD5CryptoServiceProvider MyMD5 = new MD5CryptoServiceProvider();
try
{
Byte[] MyMD5_Str = MyMD5.ComputeHash(Encoding.UTF8.GetBytes(this.md5Str));
this.MyDesStr = Encoding.UTF8.GetString(MyMD5_Str);
}
catch (Exception Error)
{
this.messAge ="MD5加密出错:"+ Error.Message;
}
}
#endregion
}
}