Advertisement
MostafaLotfy1999

fdsfd

Feb 26th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. namespace Kaibou.Network.Security
  2. {
  3. using System;
  4. using Kaibou.Network.Sockets;
  5.  
  6. public unsafe class AuthCryptography : ICipher
  7. {
  8. public class CryptCounter
  9. {
  10. public CryptCounter()
  11. {
  12.  
  13. }
  14. public CryptCounter(ushort with)
  15. {
  16. m_Counter = with;
  17. }
  18. UInt16 m_Counter = 0;
  19. public byte Key2
  20. {
  21. get { return (byte)(m_Counter >> 8); }
  22. }
  23. public byte Key1
  24. {
  25. get { return (byte)(m_Counter & 0xFF); }
  26. }
  27. public void Increment()
  28. {
  29. m_Counter++;
  30. }
  31. }
  32. private CryptCounter _decryptCounter;
  33. private CryptCounter _encryptCounter;
  34. private static byte[] _cryptKey1;
  35. private static byte[] _cryptKey2;
  36. private static byte[] _cryptKey3;
  37. private static byte[] _cryptKey4;
  38. private static bool Decrypt2 = false;
  39. public static void PrepareAuthCryptography()
  40. {
  41. if (_cryptKey1 != null)
  42. {
  43. if (_cryptKey1.Length != 0)
  44. return;
  45. }
  46. _cryptKey1 = new byte[0x100];
  47. _cryptKey2 = new byte[0x100];
  48. byte i_key1 = 0x9D;
  49. byte i_key2 = 0x62;
  50. for (int i = 0; i < 0x100; i++)
  51. {
  52. _cryptKey1[i] = i_key1;
  53. _cryptKey2[i] = i_key2;
  54. i_key1 = (byte)((0x0F + (byte)(i_key1 * 0xFA)) * i_key1 + 0x13);
  55. i_key2 = (byte)((0x79 - (byte)(i_key2 * 0x5C)) * i_key2 + 0x6D);
  56. }
  57. }
  58. public AuthCryptography()
  59. {
  60. _encryptCounter = new CryptCounter();
  61. _decryptCounter = new CryptCounter();
  62. }
  63. public byte[] Encrypt(byte[] buffer, int length)
  64. {
  65. byte[] result = new byte[length];
  66. for (int i = 0; i < length; i++)
  67. {
  68. result[i] ^= (byte)(buffer[i] ^ 0xAB);
  69. result[i] = (byte)(result[i] >> 4 | result[i] << 4);
  70. result[i] ^= (byte)(_cryptKey1[_encryptCounter.Key1] ^ _cryptKey2[_encryptCounter.Key2]);
  71. _encryptCounter.Increment();
  72. }
  73. return result;
  74. }
  75. public byte[] Decrypt(byte[] buffer, int length)
  76. {
  77. byte[] result = new byte[length];
  78. if (!Decrypt2)
  79. {
  80. for (int i = 0; i < length; i++)
  81. {
  82. result[i] ^= (byte)(buffer[i] ^ 0xAB);
  83. result[i] = (byte)(result[i] >> 4 | result[i] << 4);
  84. result[i] ^= (byte)(_cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1]);
  85. _decryptCounter.Increment();
  86. }
  87. }
  88. else
  89. {
  90. for (int i = 0; i < length; i++)
  91. {
  92. result[i] ^= (byte)(buffer[i] ^ 0xAB);
  93. result[i] = (byte)(result[i] >> 4 | result[i] << 4);
  94. result[i] ^= (byte)(_cryptKey4[_decryptCounter.Key2] ^ _cryptKey3[_decryptCounter.Key1]);
  95. _decryptCounter.Increment();
  96. }
  97. }
  98. return result;
  99. }
  100. public void Decrypt(byte[] packet, byte[] buffer, int length, int position)
  101. {
  102. if (!Decrypt2)
  103. {
  104. for (int i = position; i < length + position; i++)
  105. {
  106. packet[i] ^= (byte)(buffer[i - position] ^ 0xAB);
  107. packet[i] = (byte)(packet[i] >> 4 | packet[i] << 4);
  108. packet[i] ^= (byte)(_cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1]);
  109. _decryptCounter.Increment();
  110. }
  111. }
  112. else
  113. {
  114. for (int i = position; i < length + position; i++)
  115. {
  116. packet[i] ^= (byte)(buffer[i - position] ^ 0xAB);
  117. packet[i] = (byte)(packet[i] >> 4 | packet[i] << 4);
  118. packet[i] ^= (byte)(_cryptKey4[_decryptCounter.Key2] ^ _cryptKey3[_decryptCounter.Key1]);
  119. _decryptCounter.Increment();
  120. }
  121. }
  122. }
  123. public static void GenerateKeys(UInt32 CryptoKey, UInt32 AccountID)
  124. {
  125. UInt32 tmpkey1 = 0, tmpkey2 = 0;
  126. tmpkey1 = ((CryptoKey + AccountID) ^ (0x4321)) ^ CryptoKey;
  127. tmpkey2 = tmpkey1 * tmpkey1;
  128. _cryptKey3 = new byte[256];
  129. _cryptKey4 = new byte[256];
  130.  
  131. for (int i = 0; i < 256; i++)
  132. {
  133. int right = ((3 - (i % 4)) * 8);
  134. int left = ((i % 4)) * 8 + right;
  135. _cryptKey3[i] = (byte)(_cryptKey1[i] ^ tmpkey1 << right >> left);
  136. _cryptKey4[i] = (byte)(_cryptKey2[i] ^ tmpkey2 << right >> left);
  137. }
  138. Decrypt2 = true;
  139. }
  140. public static void GenerateKeys2(byte[] InKey1, byte[] InKey2)
  141. {
  142. byte[] addKey1 = new byte[4];
  143. byte[] addKey2 = new byte[4];
  144. byte[] addResult = new byte[4];
  145. byte[] tempKey = new byte[4];
  146. long LMULer;
  147. _cryptKey3 = new byte[256];
  148. _cryptKey4 = new byte[256];
  149. for (int x = 0; x < 4; x++)
  150. {
  151. addKey1[x] = InKey1[3 - x];
  152. addKey2[x] = InKey2[3 - x];
  153. }
  154. uint Adder1;
  155. uint Adder2;
  156. uint Adder3;
  157. Adder1 = (uint)((addKey1[3] << 24) | (addKey1[2] << 16) | (addKey1[1] << 8) | (addKey1[0]));
  158. Adder2 = (uint)((addKey2[3] << 24) | (addKey2[2] << 16) | (addKey2[1] << 8) | (addKey2[0]));
  159. Adder3 = Adder1 + Adder2;
  160. addResult[0] = (byte)(Adder3 & 0xff);
  161. addResult[1] = (byte)((Adder3 >> 8) & 0xff);
  162. addResult[2] = (byte)((Adder3 >> 16) & 0xff);
  163. addResult[3] = (byte)((Adder3 >> 24) & 0xff);
  164. for (int b = 3; b >= 0; b--)
  165. {
  166. tempKey[3 - b] = addResult[b];
  167. }
  168. tempKey[2] = (byte)(tempKey[2] ^ (byte)0x43);
  169. tempKey[3] = (byte)(tempKey[3] ^ (byte)0x21);
  170.  
  171. for (int b = 0; b < 4; b++)
  172. {
  173. tempKey[b] = (byte)(tempKey[b] ^ InKey1[b]);
  174. }
  175. for (int b = 0; b < 256; b++)
  176. {
  177. _cryptKey3[b] = (byte)(tempKey[3 - (b % 4)] ^ _cryptKey1[b]);
  178. }
  179. for (int x = 0; x < 4; x++)
  180. {
  181. addResult[x] = tempKey[3 - x];
  182. }
  183. Adder3 = (uint)((addResult[3] << 24) | (addResult[2] << 16) | (addResult[1] << 8) | (addResult[0]));
  184. LMULer = Adder3 * Adder3;
  185. LMULer = LMULer << 32;
  186. LMULer = LMULer >> 32;
  187. Adder3 = Convert.ToUInt32(LMULer & 0xffffffff);
  188. addResult[0] = (byte)(Adder3 & 0xff);
  189. addResult[1] = (byte)((Adder3 >> 8) & 0xff);
  190. addResult[2] = (byte)((Adder3 >> 16) & 0xff);
  191. addResult[3] = (byte)((Adder3 >> 24) & 0xff);
  192. for (int b = 3; b >= 0; b--)
  193. {
  194. tempKey[3 - b] = addResult[b];
  195. }
  196. for (int b = 0; b < 256; b++)
  197. {
  198. _cryptKey4[b] = Convert.ToByte(tempKey[3 - (b % 4)] ^ _cryptKey2[b]);
  199. }
  200. Decrypt2 = true;
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement