C# DES对称加密解密

        /// <summary>
/// 加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string Encryption(string str)
{
string psw;
string key = "tracymac";
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
psw = Convert.ToBase64String(ms.ToArray());
ms.Close();
}
return psw;
} /// <summary>
/// 解密
/// </summary>
/// <param name="psw"></param>
/// <returns></returns>
private string Decode(string psw)
{
string str;
string key = "tracymac";
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Convert.FromBase64String(psw);
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
}
return str;
}

更多个人工作中的项目请访问我的个人网站:www.88gis.cn

上一篇:Install Linux Kernel - AT91SAM9260EK


下一篇:Codeforces Round #359 (Div. 2) C