No Description

AES.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. namespace XYFDRQ.DBUtility
  8. {
  9. /// <summary>
  10. /// AES加密解密
  11. /// </summary>
  12. public class AES
  13. {
  14. /// <summary>
  15. /// 获取密钥
  16. /// </summary>
  17. private static string Key
  18. {
  19. get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
  20. }
  21. /// <summary>
  22. /// 获取向量
  23. /// </summary>
  24. private static string IV
  25. {
  26. get { return @"L+\~f4,Ir)b$=pkf"; }
  27. }
  28. /// <summary>
  29. /// AES加密
  30. /// </summary>
  31. /// <param name="plainStr">明文字符串</param>
  32. /// <returns>密文</returns>
  33. public static string AESEncrypt(string plainStr)
  34. {
  35. byte[] bKey = Encoding.UTF8.GetBytes(Key);
  36. byte[] bIV = Encoding.UTF8.GetBytes(IV);
  37. byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
  38. string encrypt = null;
  39. Rijndael aes = Rijndael.Create();
  40. try
  41. {
  42. using (MemoryStream mStream = new MemoryStream())
  43. {
  44. using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
  45. {
  46. cStream.Write(byteArray, 0, byteArray.Length);
  47. cStream.FlushFinalBlock();
  48. encrypt = Convert.ToBase64String(mStream.ToArray());
  49. }
  50. }
  51. }
  52. catch { }
  53. aes.Clear();
  54. return encrypt;
  55. }
  56. /// <summary>
  57. /// AES加密
  58. /// </summary>
  59. /// <param name="plainStr">明文字符串</param>
  60. /// <param name="returnNull">加密失败时是否返回 null,false 返回 String.Empty</param>
  61. /// <returns>密文</returns>
  62. public static string AESEncrypt(string plainStr, bool returnNull)
  63. {
  64. string encrypt = AESEncrypt(plainStr);
  65. return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
  66. }
  67. /// <summary>
  68. /// AES解密
  69. /// </summary>
  70. /// <param name="encryptStr">密文字符串</param>
  71. /// <returns>明文</returns>
  72. public static string AESDecrypt(string encryptStr)
  73. {
  74. string decrypt = null;
  75. Rijndael aes = Rijndael.Create();
  76. try
  77. {
  78. byte[] bKey = Encoding.UTF8.GetBytes(Key);
  79. byte[] bIV = Encoding.UTF8.GetBytes(IV);
  80. byte[] byteArray = Convert.FromBase64String(encryptStr);
  81. using (MemoryStream mStream = new MemoryStream())
  82. {
  83. using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
  84. {
  85. cStream.Write(byteArray, 0, byteArray.Length);
  86. cStream.FlushFinalBlock();
  87. decrypt = Encoding.UTF8.GetString(mStream.ToArray());
  88. }
  89. }
  90. }
  91. catch { }
  92. aes.Clear();
  93. return decrypt;
  94. }
  95. /// <summary>
  96. /// AES解密
  97. /// </summary>
  98. /// <param name="encryptStr">密文字符串</param>
  99. /// <param name="returnNull">解密失败时是否返回 null,false 返回 String.Empty</param>
  100. /// <returns>明文</returns>
  101. public static string AESDecrypt(string encryptStr, bool returnNull)
  102. {
  103. string decrypt = AESDecrypt(encryptStr);
  104. return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
  105. }
  106. }
  107. }