C#对称加密(AES加密)每次生成的结果都不同的实现思路和代码实例
作者:junjie 时间:2022-05-24 21:56:35
思路:使用随机向量,把随机向量放入密文中,每次解密时从密文中截取前16位,其实就是我们之前加密的随机向量。
代码:
public static string Encrypt(string plainText, string AESKey)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
rijndaelCipher.Key = Convert.FromBase64String(AESKey);//加解密双方约定好密钥:AESKey
rijndaelCipher.GenerateIV();
byte[] keyIv = rijndaelCipher.IV;
byte[] cipherBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, rijndaelCipher.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cipherBytes = ms.ToArray();//得到加密后的字节数组
cs.Close();
ms.Close();
}
}
var allEncrypt = new byte[keyIv.Length + cipherBytes.Length];
Buffer.BlockCopy(keyIv, 0, allEncrypt, 0, keyIv.Length);
Buffer.BlockCopy(cipherBytes, 0, allEncrypt, keyIv.Length * sizeof(byte), cipherBytes.Length);
return Convert.ToBase64String(allEncrypt);
}
public static string Decrypt(string showText, string AESKey)
{
string result = string.Empty;
try
{
byte[] cipherText = Convert.FromBase64String(showText);
int length = cipherText.Length;
SymmetricAlgorithm rijndaelCipher = Rijndael.Create();
rijndaelCipher.Key = Convert.FromBase64String(AESKey);//加解密双方约定好的密钥
byte[] iv = new byte[16];
Buffer.BlockCopy(cipherText, 0, iv, 0, 16);
rijndaelCipher.IV = iv;
byte[] decryptBytes = new byte[length - 16];
byte[] passwdText = new byte[length - 16];
Buffer.BlockCopy(cipherText, 16, passwdText, 0, length - 16);
using (MemoryStream ms = new MemoryStream(passwdText))
{
using (CryptoStream cs = new CryptoStream(ms, rijndaelCipher.CreateDecryptor(), CryptoStreamMode.Read))
{
cs.Read(decryptBytes, 0, decryptBytes.Length);
cs.Close();
ms.Close();
}
}
result = Encoding.UTF8.GetString(decryptBytes).Replace("\0", ""); ///将字符串后尾的'\0'去掉
}
catch { }
return result;
}
调用:
string jiaMi = MyAESTools.Encrypt(textBox1.Text, "abcdefgh12345678abcdefgh12345678");
string jieMi = MyAESTools.Decrypt(textBox3.Text, "abcdefgh12345678abcdefgh12345678");
标签:C#,对称加密,AES,加密
0
投稿
猜你喜欢
C#编程实现取整和取余的方法
2023-09-09 22:19:26
基于springboot i18n国际化后台多种语言设置的方式
2022-03-28 16:02:56
Android中实现TCP和UDP传输实例
2021-11-08 14:19:59
详解Android数据存储—使用SQLite数据库
2023-07-31 03:14:08
java实现打砖块游戏算法
2023-01-28 20:51:06
Springboot通过run启动web应用的方法
2021-08-30 01:57:37
Android 字符串中某个字段可点击和设置颜色的方法
2023-07-24 00:16:53
举例讲解Java的Hibernate框架中的多对一和一对多映射
2023-09-05 23:05:53
Android实现系统级悬浮按钮
2022-12-15 00:44:26
C#使用Process类调用外部程序分解
2022-01-07 16:34:11
浅谈java常用的几种线程池比较
2021-07-24 01:28:23
Android APK文件在电脑(PC虚拟机)上面运行方法
2023-10-05 08:44:06
Java将Exception信息转为String字符串的方法
2022-12-01 08:39:35
java中使用zxing批量生成二维码立牌
2021-12-31 04:31:03
详解java.lang.reflect.Modifier.isInterface()方法
2023-07-27 18:25:25
Android 5秒学会使用手势解锁功能
2023-07-11 13:48:32
Android实现通讯录效果——获取手机号码和姓名
2021-10-25 11:53:59
浅析Java Web错误/异常处理页面
2022-02-14 00:13:50
C#开发之Socket网络编程TCP/IP层次模型、端口及报文等探讨
2023-03-28 14:49:53
Android简单实现圆盘抽奖界面
2022-07-25 08:35:27