C# AES加密解密

來源:互聯網
上載者:User

標籤:

完整代碼:
 
  1. /******************************************************************
  2. * 建立人:HTL
  3. * 建立時間:2015-04-17 17:36:35
  4. * 說明:C# AES加密解密
  5. * Email:[email protected]
  6. *******************************************************************//*
  7. using System;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.IO;
  11. public class Test
  12. {
  13. public static void Main()
  14. {
  15. //密碼
  16. string password="1234567890123456";
  17. //加密初始化向量
  18. string iv=" ";
  19. string message=AESEncrypt("abcdefghigklmnopqrstuvwxyz0123456789",password,iv);
  20. Console.WriteLine(message);
  21. message=AESDecrypt("8Z3dZzqn05FmiuBLowExK0CAbs4TY2GorC2dDPVlsn/tP+VuJGePqIMv1uSaVErr",password,iv);
  22. Console.WriteLine(message);
  23. }
  24. /// <summary>
  25. /// AES加密
  26. /// </summary>
  27. /// <param name="text">加密字元</param>
  28. /// <param name="password">加密的密碼</param>
  29. /// <param name="iv">密鑰</param>
  30. /// <returns></returns>
  31. public static string AESEncrypt(string text, string password, string iv)
  32. {
  33. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  34. rijndaelCipher.Mode = CipherMode.CBC;
  35. rijndaelCipher.Padding = PaddingMode.PKCS7;
  36. rijndaelCipher.KeySize = 128;
  37. rijndaelCipher.BlockSize = 128;
  38. byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
  39. byte[] keyBytes = new byte[16];
  40. int len = pwdBytes.Length;
  41. if (len > keyBytes.Length) len = keyBytes.Length;
  42. System.Array.Copy(pwdBytes, keyBytes, len);
  43. rijndaelCipher.Key = keyBytes;
  44. byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
  45. rijndaelCipher.IV = new byte[16];
  46. ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
  47. byte[] plainText = Encoding.UTF8.GetBytes(text);
  48. byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
  49. return Convert.ToBase64String(cipherBytes);
  50. }
  51. /// <summary>
  52. /// AES解密
  53. /// </summary>
  54. /// <param name="text"></param>
  55. /// <param name="password"></param>
  56. /// <param name="iv"></param>
  57. /// <returns></returns>
  58. public static string AESDecrypt(string text, string password, string iv)
  59. {
  60. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  61. rijndaelCipher.Mode = CipherMode.CBC;
  62. rijndaelCipher.Padding = PaddingMode.PKCS7;
  63. rijndaelCipher.KeySize = 128;
  64. rijndaelCipher.BlockSize = 128;
  65. byte[] encryptedData = Convert.FromBase64String(text);
  66. byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
  67. byte[] keyBytes = new byte[16];
  68. int len = pwdBytes.Length;
  69. if (len > keyBytes.Length) len = keyBytes.Length;
  70. System.Array.Copy(pwdBytes, keyBytes, len);
  71. rijndaelCipher.Key = keyBytes;
  72. byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
  73. rijndaelCipher.IV = ivBytes;
  74. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  75. byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
  76. return Encoding.UTF8.GetString(plainText);
  77. }
  78. }
參考:          線上DEMO
AES加密CBC模式相容互連四種程式設計語言平台【PHP、Javascript、Java、C#】[C#.NET] 字串及檔案 利用 DES / AES 演算法加解密


來自為知筆記(Wiz)

C# AES加密解密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.