Utility.Encryption.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. namespace GameFramework
  9. {
  10. public static partial class Utility
  11. {
  12. /// <summary>
  13. /// 加密解密相关的实用函数。
  14. /// </summary>
  15. public static class Encryption
  16. {
  17. internal const int QuickEncryptLength = 220;
  18. /// <summary>
  19. /// 将 bytes 使用 code 做异或运算的快速版本。
  20. /// </summary>
  21. /// <param name="bytes">原始二进制流。</param>
  22. /// <param name="code">异或二进制流。</param>
  23. /// <returns>异或后的二进制流。</returns>
  24. public static byte[] GetQuickXorBytes(byte[] bytes, byte[] code)
  25. {
  26. return GetXorBytes(bytes, 0, QuickEncryptLength, code);
  27. }
  28. /// <summary>
  29. /// 将 bytes 使用 code 做异或运算的快速版本。此方法将复用并改写传入的 bytes 作为返回值,而不额外分配内存空间。
  30. /// </summary>
  31. /// <param name="bytes">原始及异或后的二进制流。</param>
  32. /// <param name="code">异或二进制流。</param>
  33. public static void GetQuickSelfXorBytes(byte[] bytes, byte[] code)
  34. {
  35. GetSelfXorBytes(bytes, 0, QuickEncryptLength, code);
  36. }
  37. /// <summary>
  38. /// 将 bytes 使用 code 做异或运算。
  39. /// </summary>
  40. /// <param name="bytes">原始二进制流。</param>
  41. /// <param name="code">异或二进制流。</param>
  42. /// <returns>异或后的二进制流。</returns>
  43. public static byte[] GetXorBytes(byte[] bytes, byte[] code)
  44. {
  45. if (bytes == null)
  46. {
  47. return null;
  48. }
  49. return GetXorBytes(bytes, 0, bytes.Length, code);
  50. }
  51. /// <summary>
  52. /// 将 bytes 使用 code 做异或运算。此方法将复用并改写传入的 bytes 作为返回值,而不额外分配内存空间。
  53. /// </summary>
  54. /// <param name="bytes">原始及异或后的二进制流。</param>
  55. /// <param name="code">异或二进制流。</param>
  56. public static void GetSelfXorBytes(byte[] bytes, byte[] code)
  57. {
  58. if (bytes == null)
  59. {
  60. return;
  61. }
  62. GetSelfXorBytes(bytes, 0, bytes.Length, code);
  63. }
  64. /// <summary>
  65. /// 将 bytes 使用 code 做异或运算。
  66. /// </summary>
  67. /// <param name="bytes">原始二进制流。</param>
  68. /// <param name="startIndex">异或计算的开始位置。</param>
  69. /// <param name="length">异或计算长度,若小于 0,则计算整个二进制流。</param>
  70. /// <param name="code">异或二进制流。</param>
  71. /// <returns>异或后的二进制流。</returns>
  72. public static byte[] GetXorBytes(byte[] bytes, int startIndex, int length, byte[] code)
  73. {
  74. if (bytes == null)
  75. {
  76. return null;
  77. }
  78. int bytesLength = bytes.Length;
  79. byte[] results = new byte[bytesLength];
  80. Array.Copy(bytes, 0, results, 0, bytesLength);
  81. GetSelfXorBytes(results, startIndex, length, code);
  82. return results;
  83. }
  84. /// <summary>
  85. /// 将 bytes 使用 code 做异或运算。此方法将复用并改写传入的 bytes 作为返回值,而不额外分配内存空间。
  86. /// </summary>
  87. /// <param name="bytes">原始及异或后的二进制流。</param>
  88. /// <param name="startIndex">异或计算的开始位置。</param>
  89. /// <param name="length">异或计算长度。</param>
  90. /// <param name="code">异或二进制流。</param>
  91. public static void GetSelfXorBytes(byte[] bytes, int startIndex, int length, byte[] code)
  92. {
  93. if (bytes == null)
  94. {
  95. return;
  96. }
  97. if (code == null)
  98. {
  99. throw new GameFrameworkException("Code is invalid.");
  100. }
  101. int codeLength = code.Length;
  102. if (codeLength <= 0)
  103. {
  104. throw new GameFrameworkException("Code length is invalid.");
  105. }
  106. if (startIndex < 0 || length < 0 || startIndex + length > bytes.Length)
  107. {
  108. throw new GameFrameworkException("Start index or length is invalid.");
  109. }
  110. int codeIndex = startIndex % codeLength;
  111. for (int i = startIndex; i < length; i++)
  112. {
  113. bytes[i] ^= code[codeIndex++];
  114. codeIndex %= codeLength;
  115. }
  116. }
  117. }
  118. }
  119. }