码迷,mamicode.com
首页 > 其他好文 > 详细

AESHelper

时间:2019-10-16 16:20:44      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:解密   class   ndt   程序   name   text   mic   转换   crypto   

 

借鉴微软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;

}

 

AESHelper

标签:解密   class   ndt   程序   name   text   mic   转换   crypto   

原文地址:https://www.cnblogs.com/MrBaHong-RJM/p/11686097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!