加密,解密

public static class CryptoHelper
{
#region 加密/解密的主要算法

private static readonly Byte[] _KeyByte = { 81, 1, 132, 230, 34, 29, 50, 200 };
private static readonly Byte[] _IVByte = { 84, 41, 32, 50, 93, 47, 4, 154 };

/// <summary>
/// 字符串加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string EncryptString(string input)
{
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
ICryptoTransform ct = csp.CreateEncryptor(_KeyByte, _IVByte);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
byte[] byt = Encoding.UTF8.GetBytes(input);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();

return Convert.ToBase64String(ms.ToArray());
}

/// <summary>
/// 字符串解密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string DecryptString(string input)
{
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
ICryptoTransform ct = csp.CreateDecryptor(_KeyByte, _IVByte);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
byte[] byt = Convert.FromBase64String(input);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();

return Encoding.UTF8.GetString(ms.ToArray());
}


#endregion
}

posted @ 2012-11-28 17:36  net_miao  阅读(152)  评论(0编辑  收藏  举报