日期:2014-05-17 浏览次数:20801 次
/// <summary>
/// 加密方法
/// </summary>
/// <param name="pToEncrypt">源字符串</param>
/// <param name="sKey">加密KEY</param>
/// <returns>加密好的字符串</returns>
public static string TripleDESEncrypt(string pToEncrypt, string sKey)
{
try
{
StringBuilder ret = new StringBuilder();
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
catch (Exception ex)
{
return ex.ToString();
}
}