LabelContainer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Xml;
  6. namespace AIPractice.Shared.Labels
  7. {
  8. public class LabelContainer
  9. {
  10. private const string _encryptKey = "VINNO123";
  11. private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  12. /// <summary>
  13. /// All labels included in this container.
  14. /// </summary>
  15. public IList<RootLabel> Labels { get; } = new List<RootLabel>();
  16. public static LabelContainer FromXml(string xmlContent)
  17. {
  18. var container = new LabelContainer();
  19. using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent)))
  20. {
  21. using (var r = XmlReader.Create(ms))
  22. {
  23. r.Read();
  24. while (r.NodeType == XmlNodeType.Element && r.Name == nameof(RootLabel) ||
  25. r.ReadToDescendant(nameof(RootLabel)))
  26. {
  27. container.Labels.Add(RootLabel.FromXml(r.ReadOuterXml()));
  28. }
  29. return container;
  30. }
  31. }
  32. }
  33. public string ToXml()
  34. {
  35. var sb = new StringBuilder();
  36. sb.Append($"<{nameof(LabelContainer)}>");
  37. foreach (var label in Labels)
  38. {
  39. sb.Append(label.ToXml());
  40. }
  41. sb.Append($"</{nameof(LabelContainer)}>");
  42. return sb.ToString();
  43. }
  44. private static byte[] Encode(string data)
  45. {
  46. try
  47. {
  48. byte[] rgbKey = Encoding.UTF8.GetBytes(_encryptKey.Substring(0, 8));
  49. byte[] rgbIV = Keys;
  50. byte[] inputByteArray = Encoding.UTF8.GetBytes(data);
  51. DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider();
  52. using (MemoryStream ms = new MemoryStream())
  53. {
  54. using (var cs = new CryptoStream(ms, dcsp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
  55. {
  56. cs.Write(inputByteArray, 0, inputByteArray.Length);
  57. cs.FlushFinalBlock();
  58. return ms.ToArray();
  59. }
  60. }
  61. }
  62. catch
  63. {
  64. return new byte[0];
  65. }
  66. }
  67. private static string Decode(byte[] data)
  68. {
  69. try
  70. {
  71. byte[] rgbKey = Encoding.UTF8.GetBytes(_encryptKey);
  72. byte[] rgbIV = Keys;
  73. byte[] inputByteArray = data;
  74. DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider();
  75. using (MemoryStream ms = new MemoryStream())
  76. {
  77. using (var cs = new CryptoStream(ms, dcsp.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
  78. {
  79. cs.Write(inputByteArray, 0, inputByteArray.Length);
  80. cs.FlushFinalBlock();
  81. return Encoding.UTF8.GetString(ms.ToArray());
  82. }
  83. }
  84. }
  85. catch
  86. {
  87. return string.Empty;
  88. }
  89. }
  90. public void Save(string fileName)
  91. {
  92. var ext = Path.GetExtension(fileName);
  93. if (ext.ToLower() == ".xml")
  94. {
  95. File.WriteAllText(fileName, ToXml());
  96. }
  97. else
  98. {
  99. var data = Encode(ToXml());
  100. File.WriteAllBytes(fileName, data);
  101. }
  102. }
  103. public static LabelContainer Open(string fileName)
  104. {
  105. var ext = Path.GetExtension(fileName);
  106. if (ext.ToLower() == ".xml")
  107. {
  108. return FromXml(File.ReadAllText(fileName));
  109. }
  110. else
  111. {
  112. var data = File.ReadAllBytes(fileName);
  113. var content = Decode(data);
  114. return FromXml(content);
  115. }
  116. }
  117. }
  118. }