XXTEA.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CP.Common
  5. {
  6. /// <summary>
  7. /// xxtea加解密
  8. /// </summary>
  9. public class XXTEA
  10. {
  11. /// <summary>
  12. /// 加/解密KEY
  13. /// </summary>
  14. private const string PASSKEY = "01F4FD6F4CB96EA4";
  15. /// <summary>
  16. /// 加密字符串
  17. /// </summary>
  18. /// <param name="Str"></param>
  19. /// <returns></returns>
  20. public static string Encode(string Str)
  21. {
  22. System.Text.Encoding encoder = System.Text.Encoding.UTF8;
  23. Byte[] data = Encrypt(encoder.GetBytes(Str), encoder.GetBytes(PASSKEY));
  24. return System.Convert.ToBase64String(data);
  25. }
  26. /// <summary>
  27. /// 解密码字符串
  28. /// </summary>
  29. /// <param name="Str"></param>
  30. /// <returns></returns>
  31. public static string Decode(string Str)
  32. {
  33. System.Text.Encoding encoder = System.Text.Encoding.UTF8;
  34. string DeStr = string.Empty;
  35. try
  36. {
  37. DeStr = encoder.GetString(Decrypt(System.Convert.FromBase64String(Str), encoder.GetBytes(PASSKEY)));
  38. }
  39. catch
  40. {
  41. return "";
  42. }
  43. return DeStr;
  44. }
  45. private static Byte[] Encrypt(Byte[] Data, Byte[] Key)
  46. {
  47. if (Data.Length == 0)
  48. {
  49. return Data;
  50. }
  51. return ToByteArray(Encrypt(ToUInt32Array(Data, true), ToUInt32Array(Key, false)), false);
  52. }
  53. private static Byte[] Decrypt(Byte[] Data, Byte[] Key)
  54. {
  55. if (Data.Length == 0)
  56. {
  57. return Data;
  58. }
  59. return ToByteArray(Decrypt(ToUInt32Array(Data, false), ToUInt32Array(Key, false)), true);
  60. }
  61. private static UInt32[] Encrypt(UInt32[] v, UInt32[] k)
  62. {
  63. Int32 n = v.Length - 1;
  64. if (n < 1)
  65. {
  66. return v;
  67. }
  68. if (k.Length < 4)
  69. {
  70. UInt32[] Key = new UInt32[4];
  71. k.CopyTo(Key, 0);
  72. k = Key;
  73. }
  74. UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum = 0, e;
  75. Int32 p, q = 6 + 52 / (n + 1);
  76. while (q-- > 0)
  77. {
  78. sum = unchecked(sum + delta);
  79. e = sum >> 2 & 3;
  80. for (p = 0; p < n; p++)
  81. {
  82. y = v[p + 1];
  83. z = unchecked(v[p] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
  84. }
  85. y = v[0];
  86. z = unchecked(v[n] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
  87. }
  88. return v;
  89. }
  90. private static UInt32[] Decrypt(UInt32[] v, UInt32[] k)
  91. {
  92. Int32 n = v.Length - 1;
  93. if (n < 1)
  94. {
  95. return v;
  96. }
  97. if (k.Length < 4)
  98. {
  99. UInt32[] Key = new UInt32[4];
  100. k.CopyTo(Key, 0);
  101. k = Key;
  102. }
  103. UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum, e;
  104. Int32 p, q = 6 + 52 / (n + 1);
  105. sum = unchecked((UInt32)(q * delta));
  106. while (sum != 0)
  107. {
  108. e = sum >> 2 & 3;
  109. for (p = n; p > 0; p--)
  110. {
  111. z = v[p - 1];
  112. y = unchecked(v[p] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
  113. }
  114. z = v[n];
  115. y = unchecked(v[0] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
  116. sum = unchecked(sum - delta);
  117. }
  118. return v;
  119. }
  120. private static UInt32[] ToUInt32Array(Byte[] Data, Boolean IncludeLength)
  121. {
  122. Int32 n = (((Data.Length & 3) == 0) ? (Data.Length >> 2) : ((Data.Length >> 2) + 1));
  123. UInt32[] Result;
  124. if (IncludeLength)
  125. {
  126. Result = new UInt32[n + 1];
  127. Result[n] = (UInt32)Data.Length;
  128. }
  129. else
  130. {
  131. Result = new UInt32[n];
  132. }
  133. n = Data.Length;
  134. for (Int32 i = 0; i < n; i++)
  135. {
  136. Result[i >> 2] |= (UInt32)Data[i] << ((i & 3) << 3);
  137. }
  138. return Result;
  139. }
  140. private static Byte[] ToByteArray(UInt32[] Data, Boolean IncludeLength)
  141. {
  142. Int32 n;
  143. if (IncludeLength)
  144. {
  145. n = (Int32)Data[Data.Length - 1];
  146. }
  147. else
  148. {
  149. n = Data.Length << 2;
  150. }
  151. Byte[] Result = new Byte[n];
  152. for (Int32 i = 0; i < n; i++)
  153. {
  154. Result[i] = (Byte)(Data[i >> 2] >> ((i & 3) << 3));
  155. }
  156. return Result;
  157. }
  158. }
  159. }