C# DES 加密
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// DES加密
/// </summary>
/// <param name="toEncryptString">要加密的字符串</param>
/// <param name="keyString">密钥(必须为8位)</param>
/// <returns>以Base64格式返回的加密字符串</returns>
public static string DESEncrypt(string toEncryptString, string keyString)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] toEncryptBytes = Encoding.UTF8.GetBytes(toEncryptString);
des.Key = ASCIIEncoding.ASCII.GetBytes(keyString);
des.IV = ASCIIEncoding.ASCII.GetBytes(keyString);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(toEncryptBytes, 0, toEncryptBytes.Length);
cs.FlushFinalBlock();
cs.Close();
}
string encryptedString = Convert.ToBase64String(ms.ToArray());
ms.Close();
return encryptedString;
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="toDecryptString">要解密的Base64字符串</param>
/// <param name="keyString">密钥(必须为8位)</param>
/// <returns>已解密的字符串</returns>
public static string DESDecrypt(string toDecryptString, string keyString)
{
byte[] toDecryptBytes = Convert.FromBase64String(toDecryptString);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(keyString);
des.IV = ASCIIEncoding.ASCII.GetBytes(keyString);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(toDecryptBytes, 0, toDecryptBytes.Length);
cs.FlushFinalBlock();
cs.Close();
}
string decryptedString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return decryptedString;
}
}
/// DES加密
/// </summary>
/// <param name="toEncryptString">要加密的字符串</param>
/// <param name="keyString">密钥(必须为8位)</param>
/// <returns>以Base64格式返回的加密字符串</returns>
public static string DESEncrypt(string toEncryptString, string keyString)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] toEncryptBytes = Encoding.UTF8.GetBytes(toEncryptString);
des.Key = ASCIIEncoding.ASCII.GetBytes(keyString);
des.IV = ASCIIEncoding.ASCII.GetBytes(keyString);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(toEncryptBytes, 0, toEncryptBytes.Length);
cs.FlushFinalBlock();
cs.Close();
}
string encryptedString = Convert.ToBase64String(ms.ToArray());
ms.Close();
return encryptedString;
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="toDecryptString">要解密的Base64字符串</param>
/// <param name="keyString">密钥(必须为8位)</param>
/// <returns>已解密的字符串</returns>
public static string DESDecrypt(string toDecryptString, string keyString)
{
byte[] toDecryptBytes = Convert.FromBase64String(toDecryptString);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(keyString);
des.IV = ASCIIEncoding.ASCII.GetBytes(keyString);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(toDecryptBytes, 0, toDecryptBytes.Length);
cs.FlushFinalBlock();
cs.Close();
}
string decryptedString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return decryptedString;
}
}
要引用 System.Security.Cryptography 命名空间。