C#实现对AES加密和解密的方法
时间:2021-07-27 16:34:24
AES简介
AES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范。它被预期能成为人们公认的加密包括金融、电信和政府数字信息的方法。
AES 是一个新的可以用于保护电子数据的加密算法。明确地说,AES 是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据。与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据。通过分组密码返回的加密数据 的位数与输入数据相同。以下是我经过整理的代码,希望对大家有所帮助:
/// <summary>
/// ASE加解密
/// </summary>
public class AESHelper
{
/// <summary>
/// 获取密钥
/// </summary>
private static string Key
{
get
{
return "abcdef1234567890"; ////必须是16位
}
}
//默认密钥向量
private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// AES加密算法
/// </summary>
/// <param name="plainText">明文字符串</param>
/// <returns>将加密后的密文转换为Base64编码,以便显示</returns>
public static string AESEncrypt(string plainText)
{
//分组加密算法
SymmetricAlgorithm des = Rijndael.Create();
byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
//设置密钥及密钥向量
des.Key = Encoding.UTF8.GetBytes(Key);
des.IV = _key1;
byte[] cipherBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cipherBytes = ms.ToArray();//得到加密后的字节数组
cs.Close();
ms.Close();
}
}
return Convert.ToBase64String(cipherBytes);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="cipherText">密文字符串</param>
/// <returns>返回解密后的明文字符串</returns>
public static string AESDecrypt(string showText)
{
byte[] cipherText = Convert.FromBase64String(showText);
SymmetricAlgorithm des = Rijndael.Create();
des.Key = Encoding.UTF8.GetBytes(Key);
des.IV = _key1;
byte[] decryptBytes = new byte[cipherText.Length];
using (MemoryStream ms = new MemoryStream(cipherText))
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
{
cs.Read(decryptBytes, 0, decryptBytes.Length);
cs.Close();
ms.Close();
}
}
return Encoding.UTF8.GetString(decryptBytes).Replace("\0", ""); ///将字符串后尾的'\0'去掉
}
}
Key的值可以放在config文件中,也可放入数据库中。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现将Email地址转成图片显示的方法
Java杂谈之重复代码是什么
Java中List集合去重方法以及效率对比
Java使用RedisTemplate如何根据前缀获取key列表
![](https://img.aspxhome.com/file/2023/9/78909_0s.png)
Java实现简单台球游戏
![](https://img.aspxhome.com/file/2023/1/62371_0s.png)
IDEA中设置代码自动提示为Alt+/的具体做法
![](https://img.aspxhome.com/file/2023/9/69889_0s.jpg)
MyBatis图文并茂讲解注解开发多对多查询
![](https://img.aspxhome.com/file/2023/1/67991_0s.png)
C#4.0新特性之协变与逆变实例分析
Java类锁、对象锁、私有锁冲突测试
Java Spring的核心与设计思想你知道吗
![](https://img.aspxhome.com/file/2023/0/66070_0s.png)
C#根据年月日计算星期几的函数
Jaxb2实现JavaBean与xml互转的方法详解
centos 安装java环境的多种方法
SpringBoot+Dubbo+Zookeeper实现简单分布式开发的应用详解
![](https://img.aspxhome.com/file/2023/8/83158_0s.png)
使用Java实现qq邮箱发送邮件
![](https://img.aspxhome.com/file/2023/1/58561_0s.jpg)
完美解决idea创建文件时,文件不分级展示的情况
![](https://img.aspxhome.com/file/2023/9/62099_0s.jpg)
Unity OnGUI实时显示游戏FPS
Java详解Swing中的几种常用按钮的使用
![](https://img.aspxhome.com/file/2023/7/76197_0s.png)
JDBC连接mysql乱码异常问题处理总结
![](https://img.aspxhome.com/file/2023/8/67068_0s.png)