日期:2014-05-17 浏览次数:20955 次
/// <summary>
/// DES加密解密算法
/// </summary>
public class DES
{
/// <summary>
/// DES加密算法
/// sKey为8位或16位
/// </summary>
/// <param name="pToEncrypt">需要加密的字符串</param>
/// <param name="sKey">密钥</param>
/// <returns></returns>
public string DesEncrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
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())
{
ret.AppendFormat("{0:X2}", b);