DecryptMiddleware.cs 5.3 KB

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