当前位置:网站首页>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 .
边栏推荐
- How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
- The width of the picture in rich text used by wechat applet exceeds the problem
- Considerations for testing a website
- How to solve the component conflicts caused by scrollbars in GridView
- 2022.7.3-----leetcode. five hundred and fifty-six
- MySQL information_ Schema database
- 198. House raiding
- MySQL的information_schema数据库
- Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
猜你喜欢
注释与注解
win10清除快速访问-不留下痕迹
C實現貪吃蛇小遊戲
SQL join, left join, right join usage
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
《ClickHouse原理解析与应用实践》读书笔记(4)
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
17-18. Dependency scope and life cycle plug-ins
C language exercises (recursion)
Configure cross compilation tool chain and environment variables
随机推荐
MySQL learning notes 3 - JDBC
Component、Container容器常用API详解:Frame、Panel、ScrollPane
Abap:ooalv realizes the function of adding, deleting, modifying and checking
分布式CAP理论
JS how to convert seconds into hours, minutes and seconds display
Qt发布多语言国际化翻译
27-31. Dependency transitivity, principle
gslb(global server load balance)技术的一点理解
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
C language - Blue Bridge Cup - Snake filling
Sort list tool class, which can sort strings
Distributed cap theory
win10清除快速访问-不留下痕迹
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
How to expand all collapse panels
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
C语言练习题(递归)
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
Leetcode question brushing record | 206_ Reverse linked list
The width of the picture in rich text used by wechat applet exceeds the problem