12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using Vinno.IUS.Common.Utilities;
- namespace Vinno.IUS.Common.Network.Transfer
- {
- public static class MessageExctension
- {
- /// <summary>
- /// 遍历DictionaryElement中的String进行加密或解密
- /// </summary>
- /// <param name="dictionaryElement"></param>
- /// <returns></returns>
- private static DictionaryElement ForeachElement(DictionaryElement dictionaryElement, ListElement listElement)
- {
- try
- {
- var elementValue = dictionaryElement?.Value;
- var list = elementValue.Values;
- foreach (var el in elementValue.Values)
- {
- var tag = el.Tag;
- var element = el.Element;
- if (element.TransferType == TransferType.String)
- {
- var e = (StringElement)element;
- var value = e.Value;
- value = DesBuilder.Decrypt(value);
- e.Value = value;
- }
- else if (el.Element is DictionaryElement element1)
- {
- ForeachElement(element1, null);
- }
- else if (el.Element is ListElement listElement1)
- {
- ForeachElement(null, listElement1);
- }
- }
- return dictionaryElement;
- }
- catch (Exception e)
- {
- Console.WriteLine("Error:" + e.ToString());
- return dictionaryElement;
- }
- }
- /// <summary>
- /// 对Message中Element的String解密
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- public static void Decrypt(this Message message)
- {
- ForeachElement(message.Element);
- }
- private static void ForeachElement(IElement element)
- {
- if (element is DictionaryElement dictionaryElement)
- {
- foreach (var childElement in dictionaryElement.Value.Values)
- {
- ForeachElement(childElement.Element);
- }
- }
- else if (element is ListElement listElement)
- {
- foreach (var listChildElement in listElement.Value)
- {
- ForeachElement(listChildElement.Element);
- }
- }
- else if (element.TransferType == TransferType.String)
- {
- var e = (StringElement)element;
- var value = e.Value;
- value = DesBuilder.Decrypt(value);
- e.Value = value;
- }
- }
- }
- }
|