LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#對(duì)稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例

admin
2023年3月22日 18:28 本文熱度 1275

這篇文章主要介紹了C#對(duì)稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例,每次解密時(shí)從密文中截取前16位,這就是實(shí)現(xiàn)隨機(jī)的奧秘,本文同時(shí)給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下。

思路:使用隨機(jī)向量,把隨機(jī)向量放入密文中,每次解密時(shí)從密文中截取前16位,其實(shí)就是我們之前加密的隨機(jī)向量。

代碼:

  1. public static string Encrypt(string plainText, string AESKey) 

  2. RijndaelManaged rijndaelCipher = new RijndaelManaged(); 

  3. byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字節(jié)數(shù)組 

  4. rijndaelCipher.Key = Convert.fromBase64String(AESKey);//加解密雙方約定好密鑰:AESKey 

  5. rijndaelCipher.GenerateIV(); 

  6. byte[] keyIv = rijndaelCipher.IV; 

  7. byte[] cipherBytes = null

  8. using (MemoryStream ms = new MemoryStream()) 

  9. using (CryptoStream cs = new CryptoStream(ms, rijndaelCipher.createEncryptor(), CryptoStreamMode.Write)) 

  10. cs.Write(inputByteArray, 0, inputByteArray.Length); 

  11. cs.FlushFinalBlock(); 

  12. cipherBytes = ms.ToArray();//得到加密后的字節(jié)數(shù)組 

  13. cs.Close(); 

  14. ms.Close(); 

  15. var allEncrypt = new byte[keyIv.Length + cipherBytes.Length]; 

  16. Buffer.BlockCopy(keyIv, 0, allEncrypt, 0, keyIv.Length); 

  17. Buffer.BlockCopy(cipherBytes, 0, allEncrypt, keyIv.Length * sizeof(byte), cipherBytes.Length); 

  18. return Convert.ToBase64String(allEncrypt); 

  19.  

  20. public static string Decrypt(string showText, string AESKey) 

  21. string result = string.Empty; 

  22. try 

  23. byte[] cipherText = Convert.fromBase64String(showText); 

  24. int length = cipherText.Length; 

  25. SymmetricAlgorithm rijndaelCipher = Rijndael.create(); 

  26. rijndaelCipher.Key = Convert.fromBase64String(AESKey);//加解密雙方約定好的密鑰 

  27. byte[] iv = new byte[16]; 

  28. Buffer.BlockCopy(cipherText, 0, iv, 0, 16); 

  29. rijndaelCipher.IV = iv; 

  30. byte[] decryptBytes = new byte[length - 16]; 

  31. byte[] passwdText = new byte[length - 16]; 

  32. Buffer.BlockCopy(cipherText, 16, passwdText, 0, length - 16); 

  33. using (MemoryStream ms = new MemoryStream(passwdText)) 

  34. using (CryptoStream cs = new CryptoStream(ms, rijndaelCipher.createDecryptor(), CryptoStreamMode.Read)) 

  35. cs.Read(decryptBytes, 0, decryptBytes.Length); 

  36. cs.Close(); 

  37. ms.Close(); 

  38. result = Encoding.UTF8.GetString(decryptBytes).Replace("/0"""); ///將字符串后尾的'/0'去掉 

  39. catch { } 

  40. return result; 

調(diào)用: 

  1. string jiaMi = MyAESTools.Encrypt(textBox1.Text, "abcdefgh12345678abcdefgh12345678"); 

  2.  

  3. string jieMi = MyAESTools.Decrypt(textBox3.Text, "abcdefgh12345678abcdefgh12345678"); 


該文章在 2023/3/22 18:28:42 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
中文手机字幕大香视频蕉 | 中文字幕乱码亚州无线码二区 | 最新在线精品国自产拍视频 | 天天噜啦在线播放 | 亚洲熟女综合一区二区三区 | 日韩国产亚洲欧美在线 |