DecryptMiddleware.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Text;
  2. using System.Web;
  3. using Common;
  4. using Infrastructure;
  5. namespace Filters
  6. {
  7. public class DecryptMiddleware
  8. {
  9. public static int Do(HttpContext context)
  10. {
  11. var request = context.Request;
  12. string cipherText = "";
  13. try
  14. {
  15. if(request.Method.ToLower() == "get")
  16. {
  17. cipherText = request.Query["value"];
  18. cipherText = HttpUtility.UrlDecode(cipherText);
  19. cipherText = Decrypt(cipherText);
  20. cipherText = HttpUtility.UrlDecode(cipherText);
  21. }
  22. else if(request.Method.ToLower() == "delete")
  23. {
  24. string path = request.Path.Value;
  25. string value = path.Substring(path.LastIndexOf("/") + 1);
  26. path = path.Substring(0, path.LastIndexOf("/") + 1);
  27. value = Decrypt(value);
  28. path += value;
  29. request.Path = new PathString(path);
  30. request.RouteValues["id"] = value;
  31. }
  32. else
  33. {
  34. using var reader = new StreamReader(request.Body, leaveOpen: true);
  35. cipherText = reader.ReadToEnd();
  36. if(cipherText.StartsWith("value="))
  37. {
  38. cipherText = cipherText.Replace("value=", "");
  39. }
  40. cipherText = Decrypt(cipherText);
  41. }
  42. if (string.IsNullOrWhiteSpace(cipherText))
  43. {
  44. return 1;
  45. }
  46. if(request.Method.ToLower() == "get")
  47. {
  48. cipherText = GetParams(cipherText);
  49. var queryDict = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(cipherText);
  50. var newQuery = new QueryCollection(queryDict);
  51. request.Query = newQuery;
  52. request.QueryString = new QueryString("?" + cipherText);
  53. }
  54. else if(request.Method.ToLower() == "delete") {}
  55. else
  56. {
  57. var bytes = Encoding.UTF8.GetBytes(cipherText);
  58. var ms = new MemoryStream(bytes);
  59. request.Body = ms; // 替换为明文
  60. request.ContentLength = bytes.Length;
  61. ms.Position = 0;
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. context.Response.StatusCode = StatusCodes.Status400BadRequest;
  67. Utils.WriteLog(ex.ToString(), "解密异常");
  68. return 2;
  69. }
  70. return 0;
  71. }
  72. public static string Decrypt(string str)
  73. {
  74. if(string.IsNullOrEmpty(str)) return "";
  75. str = str.Trim('"');
  76. str = HttpUtility.UrlDecode(str);
  77. str = Encoding.UTF8.GetString(Convert.FromBase64String(str));
  78. var options = App.OptionsSetting;
  79. var aes = options.Aes;
  80. return Dbconn.AesDecrypt(str, aes.EncodeKey, aes.EncodeIv, aes.EncodeMode, aes.EncodePadding);
  81. }
  82. #region 接口通用DES解密
  83. public static string DesDecrypt(string content)
  84. {
  85. content = HttpUtility.UrlDecode(content);
  86. return Dbconn.DesDecrypt(content, "&L^kg4N9");
  87. }
  88. #endregion
  89. public static string GetParam(string content, string key)
  90. {
  91. if(content.StartsWith("?")) content = content.Substring(1);
  92. string[] data = content.Split('&');
  93. foreach(string sub in data)
  94. {
  95. if(sub.StartsWith(key + "="))
  96. {
  97. return sub.Substring(sub.IndexOf("=") + 1);
  98. }
  99. }
  100. return "";
  101. }
  102. public static string GetParams(string content)
  103. {
  104. SortedList<string, string> req = Newtonsoft.Json.JsonConvert.DeserializeObject<SortedList<string, string>>(content);
  105. return Function.BuildQueryString(req);
  106. }
  107. }
  108. }