MessageExctension.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using Vinno.IUS.Common.Utilities;
  3. namespace Vinno.IUS.Common.Network.Transfer
  4. {
  5. public static class MessageExctension
  6. {
  7. /// <summary>
  8. /// 遍历DictionaryElement中的String进行加密或解密
  9. /// </summary>
  10. /// <param name="dictionaryElement"></param>
  11. /// <returns></returns>
  12. private static DictionaryElement ForeachElement(DictionaryElement dictionaryElement, ListElement listElement)
  13. {
  14. try
  15. {
  16. var elementValue = dictionaryElement?.Value;
  17. var list = elementValue.Values;
  18. foreach (var el in elementValue.Values)
  19. {
  20. var tag = el.Tag;
  21. var element = el.Element;
  22. if (element.TransferType == TransferType.String)
  23. {
  24. var e = (StringElement)element;
  25. var value = e.Value;
  26. value = DesBuilder.Decrypt(value);
  27. e.Value = value;
  28. }
  29. else if (el.Element is DictionaryElement element1)
  30. {
  31. ForeachElement(element1, null);
  32. }
  33. else if (el.Element is ListElement listElement1)
  34. {
  35. ForeachElement(null, listElement1);
  36. }
  37. }
  38. return dictionaryElement;
  39. }
  40. catch (Exception e)
  41. {
  42. Console.WriteLine("Error:" + e.ToString());
  43. return dictionaryElement;
  44. }
  45. }
  46. /// <summary>
  47. /// 对Message中Element的String解密
  48. /// </summary>
  49. /// <param name="message"></param>
  50. /// <returns></returns>
  51. public static void Decrypt(this Message message)
  52. {
  53. ForeachElement(message.Element);
  54. }
  55. private static void ForeachElement(IElement element)
  56. {
  57. if (element is DictionaryElement dictionaryElement)
  58. {
  59. foreach (var childElement in dictionaryElement.Value.Values)
  60. {
  61. ForeachElement(childElement.Element);
  62. }
  63. }
  64. else if (element is ListElement listElement)
  65. {
  66. foreach (var listChildElement in listElement.Value)
  67. {
  68. ForeachElement(listChildElement.Element);
  69. }
  70. }
  71. else if (element.TransferType == TransferType.String)
  72. {
  73. var e = (StringElement)element;
  74. var value = e.Value;
  75. value = DesBuilder.Decrypt(value);
  76. e.Value = value;
  77. }
  78. }
  79. }
  80. }