郑许地铁

AES256Controller.cs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using ZXDT.CallCenter.MVCWeb.Controllers;
  10. namespace CallCenterApi.Interface.Controllers.Login
  11. {
  12. public class AES256Controller : BaseController
  13. {
  14. // GET: AES256
  15. public ActionResult Index()
  16. {
  17. return View();
  18. }
  19. private static string Key
  20. {
  21. //get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
  22. get { return @")O[9d]6,YF}+efcaj{+8>Z'e9M"; }
  23. }
  24. /// <summary>
  25. /// AES加密
  26. /// </summary>
  27. /// <param name="encryptStr">明文</param>
  28. /// <param name="key">32位密钥</param>
  29. /// <returns></returns>
  30. public string Encrypt(string encryptStr,string ltime)
  31. {
  32. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key+ltime);
  33. byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
  34. RijndaelManaged rDel = new RijndaelManaged();
  35. rDel.Key = keyArray;
  36. rDel.Mode = CipherMode.ECB;
  37. rDel.Padding = PaddingMode.PKCS7;
  38. ICryptoTransform cTransform = rDel.CreateEncryptor();
  39. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  40. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  41. }
  42. /// <summary>
  43. /// AES解密
  44. /// </summary>
  45. /// <param name="decryptStr">密文</param>
  46. /// <param name="key">密钥</param>
  47. /// <returns></returns>
  48. public string Decrypt(string decryptStr, string ltime)
  49. {
  50. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key+ltime );
  51. byte[] toEncryptArray = Convert.FromBase64String(decryptStr);
  52. RijndaelManaged rDel = new RijndaelManaged();
  53. rDel.Key = keyArray;
  54. rDel.Mode = CipherMode.ECB;
  55. rDel.Padding = PaddingMode.PKCS7;
  56. ICryptoTransform cTransform = rDel.CreateDecryptor();
  57. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  58. return UTF8Encoding.UTF8.GetString(resultArray);
  59. }
  60. }
  61. }