当前位置:网站首页>C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
2022-07-04 06:21:00 【Brother Lei talks about programming】
Ideas : Use random vectors , Put the random vector into the ciphertext , Before intercepting from the ciphertext each time decryption 16 position , In fact, it is the random vector we encrypted before .
Code
public static string Encrypt(string plainText, string AESKey)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);// Get the byte array to be encrypted
rijndaelCipher.Key = Convert.FromBase64String(AESKey);// The encryption and decryption parties agree on the key :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();// Get the encrypted byte array
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);// The encryption and decryption key agreed by both parties
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", ""); /// End of string '\0' Get rid of
}
catch { }
return result;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
call :
string jiaMi = MyAESTools.Encrypt(textBox1.Text, "abcdefgh12345678abcdefgh12345678");
string jieMi = MyAESTools.Decrypt(textBox3.Text, "abcdefgh12345678abcdefgh12345678");
- 1.
- 2.
- 3.
Follow the QR code below , Subscribe to more .
边栏推荐
- Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
- 509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
- JS flattened array of number shape structure
- Recommended system 1 --- framework
- AWT常用组件、FileDialog文件选择框
- MySQL learning notes 3 - JDBC
- 【无标题】
- win10清除快速访问-不留下痕迹
- buuctf-pwn write-ups (8)
- Native Cloud - SSH articles must be read on Cloud (used for Remote Login to Cloud Server)
猜你喜欢
C语言练习题(递归)
QT 获取随机颜色值设置label背景色 代码
树形dp
Bicolor case
17-18. Dependency scope and life cycle plug-ins
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
Sword finger offer II 038 Daily temperature
【微服务】Nacos集群搭建以及加载文件配置
剑指 Offer II 038. 每日温度
4G wireless all network solar hydrological equipment power monitoring system bms110
随机推荐
2022.7.2-----leetcode. eight hundred and seventy-one
QT get random color value and set label background color code
JS execution mechanism
实用的小工具指令
2022.7.2-----leetcode.871
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
QT QTableWidget 表格列置顶需求的思路和代码
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
QT releases multilingual International Translation
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
C language exercises (recursion)
746. Climb stairs with minimum cost
JSON Web Token----JWT和传统session登录认证对比
How to expand all collapse panels
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
Nexus 6p downgraded from 8.0 to 6.0+root
MySQL learning notes 3 - JDBC
How to help others effectively