八紘

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

借鉴微软RijndaelManaged Class的例子写的,同事注明每个阶段都做了什么

https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rijndaelmanaged?redirectedfrom=MSDN&view=netframework-4.8

 里面做了一些调整和修改 希望对大家有帮助.

 

/// <summary>
/// AES加密
/// </summary>
/// <param name="plainText">加密内容</param>
/// <param name="Key">秘钥</param>
/// <param name="IV">秘钥向量</param>
/// <returns></returns>
public static String EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// 验证参数
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");

byte[] encrypted;//二进制加密值

// 创建Rijndael算法对象
// 使用指定的密钥和iv。
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

//创建解密程序以执行流转换.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

//创建内存流
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//写入数据.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}

//Base64 转义二进制组输出String值
return Convert.ToBase64String(encrypted);
}

/// <summary>
/// AES解密
/// </summary>
/// <param name="cipherText">解密字符串值</param>
/// <param name="Key">秘钥</param>
/// <param name="IV">秘钥向量</param>
/// <returns></returns>
public static string DecryptStringFromBytes(String cipherText, byte[] Key, byte[] IV)
{
// 验证参数.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");

string plaintext = null;//解密值

// 创建Rijndael算法对象
// 使用指定的密钥和iv。
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

//创建解密程序以执行流转换
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

//通过Base64给String转成二进制组,前提字符串也是由Base64转义
byte[] cipherByteArray = Convert.FromBase64String(cipherText);

//创建内存
using (MemoryStream msDecrypt = new MemoryStream(cipherByteArray))
{
// 通过CryptoStream对象,操作ICryptoTransform单向的加密---创建加密流
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
//读取内存
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}

}

return plaintext;

}

 

测试

string original = txt_str.Text;

using (RijndaelManaged myRijndael = new RijndaelManaged())
{

myRijndael.GenerateKey();
myRijndael.GenerateIV();

String encrypted = AESHelper.EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
txt_EncryptStr.Text = encrypted;

string roundtrip = AESHelper.DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

txt_str2.Text = roundtrip;

}

 

posted on 2019-10-16 16:05  狮歌吼吼  阅读(368)  评论(0)    收藏  举报