DecryptMiddleware.cs 4.4 KB

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