#region 加密解密
static string encryptKey= “Oyea”;
#region 加密字符串 public static string Encrypt(string str)
/// <summary>
/// 加密字符串
/// </summary>
/// <param name=”str”>要加密的字符串</param>
/// <returns>返回加密后的字符串</returns>
public static string Encrypt(string str)
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); //实例化加/解密类东西
byte[] key = Encoding.Unicode.GetBytes(encryptKey); //界说字节数组,用来存储密钥
byte[] data = Encoding.Unicode.GetBytes(str);//界说字节数组,,用来存储要加密的字符串
MemoryStream MStream = new MemoryStream();//实例化内存流东西
//使用内存流实例化加密流东西
CryptoStream CStream = new CryptoStream(MStream,descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data,0, data.Length); //向加密流中写入数据
CStream.FlushFinalBlock(); //释放加密流
return Convert.ToBase64String(MStream.ToArray());//返回加密后的字符串
}
#endregion
#region 解密字符串 public static string Decrypt(string str)
/// <summary>
/// 解密字符串
/// </summary>
/// <param name=”str”>要解密的字符串</param>
/// <returns>返回解密后的字符串</returns>
public static string Decrypt(string str)
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); //实例化加/解密类东西
byte[] key = Encoding.Unicode.GetBytes(encryptKey); //界说字节数组,用来存储密钥
byte[] data = Convert.FromBase64String(str);//界说字节数组,用来存储要解密的字符串
MemoryStream MStream = new MemoryStream();//实例化内存流东西
//使用内存流实例化解密流东西
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data,0, data.Length); //向解密流中写入数据
CStream.FlushFinalBlock(); //释放解密流
return Encoding.Unicode.GetString(MStream.ToArray()); //返回解密后的字符串
}
#endregion
#endregion