终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

81 line
3.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace BPASmartClient.Helper
  7. {
  8. /// <summary>
  9. /// 创建AES加密解密 add fyf 20211122
  10. /// </summary>
  11. public class AESHelper
  12. {
  13. /// <summary>
  14. /// 默认密钥-密钥的长度必须是32
  15. /// </summary>
  16. private const string PublicKey = "9848461354184618";
  17. /// <summary>
  18. /// 默认向量
  19. /// </summary>
  20. private const string Iv = "dfkladnasldnfdcv";
  21. /// <summary>
  22. /// AES加密
  23. /// </summary>
  24. /// <param name="str">需要加密字符串</param>
  25. /// <returns>加密后字符串</returns>
  26. public static String Encrypt(string str)
  27. {
  28. return Encrypt(str, PublicKey);
  29. }
  30. /// <summary>
  31. /// AES解密
  32. /// </summary>
  33. /// <param name="str">需要解密字符串</param>
  34. /// <returns>解密后字符串</returns>
  35. public static String Decrypt(string str)
  36. {
  37. return Decrypt(str, PublicKey);
  38. }
  39. /// <summary>
  40. /// AES加密
  41. /// </summary>
  42. /// <param name="str">需要加密的字符串</param>
  43. /// <param name="key">32位密钥</param>
  44. /// <returns>加密后的字符串</returns>
  45. public static string Encrypt(string str, string key)
  46. {
  47. Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
  48. Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(str);
  49. var rijndael = new System.Security.Cryptography.RijndaelManaged();
  50. rijndael.Key = keyArray;
  51. rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
  52. rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
  53. rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
  54. System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateEncryptor();
  55. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  56. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  57. }
  58. /// <summary>
  59. /// AES解密
  60. /// </summary>
  61. /// <param name="str">需要解密的字符串</param>
  62. /// <param name="key">32位密钥</param>
  63. /// <returns>解密后的字符串</returns>
  64. public static string Decrypt(string str, string key)
  65. {
  66. Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
  67. Byte[] toEncryptArray = Convert.FromBase64String(str);
  68. var rijndael = new System.Security.Cryptography.RijndaelManaged();
  69. rijndael.Key = keyArray;
  70. rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
  71. rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
  72. rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
  73. System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateDecryptor();
  74. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  75. return System.Text.Encoding.UTF8.GetString(resultArray);
  76. }
  77. }
  78. }