C# DES (cbc) 加密解密 (最简单的加密解密)
https://www.cnblogs.com/tianma3798/p/8807906.html 感谢这位大哥的代码提示
微软官网的这个示例正的是该换了,一点用处没看出来
https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.descryptoserviceprovider.-ctor?view=netframework-4.6#System_Security_Cryptography_DESCryptoServiceProvider__ctor
/// <summary>
/// DES CBC 加密
/// </summary>
/// <param name="encryStr"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
public static string DesCBCEncryptor(string encryStr,string key,string iv)
{
byte[] rgbKey = Encoding.UTF8.GetBytes(key);
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryStr);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
{
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
byte[] srteamArray = mStream.ToArray();
mStream.Close();
mStream.Dispose();
return Convert.ToBase64String(srteamArray);
}
}
}
/// <summary>
/// DES CBC 解密
/// </summary>
/// <param name="decryStr"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
public static string DesCBCDecryptor(string decryStr, string key, string iv)
{
byte[] rgbKey = Encoding.UTF8.GetBytes(key);
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
byte[] inputByteArray = Convert.FromBase64String(decryStr);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
{
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
byte[] srteamArray = mStream.ToArray();
mStream.Close();
mStream.Dispose();
return Encoding.UTF8.GetString(srteamArray);
}
}
}