#region DES加密
/// <summary>
/// DES加密
/// </summary>
/// <param name="datastr">需要加密的字符串</param>
/// <returns>返回加密后的字符串</returns>
public static string Encrypt(string datastr)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes("", null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
byte[] data = System.Text.Encoding.Unicode.GetBytes(datastr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
#endregion
#region 解密字符串
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="datastr">需要解密的字符串</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string datastr)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes("", null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
byte[] data = System.Text.Encoding.Unicode.GetBytes(datastr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
#endregion