日期:2014-05-17 浏览次数:20440 次
#region 加密解密
/// <summary>
/// DES对称加密方法
/// </summary>
/// <param name="InitData">原始待加密数据</param>
/// <param name="SecretKey">加密密钥,密钥长度必须为八位有效英文字符</param>
public string EncryptData(object InitData, string SecretKey)
{
try
{
string _newsecretkey = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(SecretKey + "123456(your key)", "MD5").ToLower();
string newSecretKey = _newsecretkey.Substring(12, 4) + _newsecretkey.Substring(25, 4);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
Byte[] inputByteArray = Encoding.Default.GetBytes(InitData.ToString());
//建立加密对象的密钥和偏移量
des.Key = ASCIIEncoding.ASCII.GetBytes(newSecretKey);
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
des.IV = ASCIIEncoding.ASCII.GetBytes(newSecretKey);
//使得输入密码必须输入英文文本
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (Byte b in ms.ToArray())
&n