NewCrypt.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util.crypt;
  16. /**
  17. * Class to use a blowfish cipher with ECB processing.<br>
  18. * Static methods are present to append/check the checksum of<br>
  19. * packets exchanged between the following partners:<br>
  20. * Login Server <-> Game Client<br>
  21. * Login Server <-> Game Server<br>
  22. * Also a static method is provided for the initial xor encryption between Login Server <-> Game Client.
  23. */
  24. public final class NewCrypt
  25. {
  26. private final BlowfishEngine _cipher;
  27. /**
  28. * @param blowfishKey
  29. */
  30. public NewCrypt(byte[] blowfishKey)
  31. {
  32. _cipher = new BlowfishEngine();
  33. _cipher.init(blowfishKey);
  34. }
  35. public NewCrypt(String key)
  36. {
  37. this(key.getBytes());
  38. }
  39. /**
  40. * Equivalent to calling {@link #verifyChecksum(byte[], int, int)} with parameters (raw, 0, raw.length)
  41. * @param raw data array to be verified
  42. * @return true when the checksum of the data is valid, false otherwise
  43. * @see #verifyChecksum(byte[], int, int)
  44. */
  45. public static boolean verifyChecksum(final byte[] raw)
  46. {
  47. return NewCrypt.verifyChecksum(raw, 0, raw.length);
  48. }
  49. /**
  50. * Method to verify the checksum of a packet received by login server from game client.<br>
  51. * This is also used for game server <-> login server communication.
  52. * @param raw data array to be verified
  53. * @param offset at which offset to start verifying
  54. * @param size number of bytes to verify
  55. * @return true if the checksum of the data is valid, false otherwise
  56. */
  57. public static boolean verifyChecksum(final byte[] raw, final int offset, final int size)
  58. {
  59. // check if size is multiple of 4 and if there is more then only the checksum
  60. if (((size & 3) != 0) || (size <= 4))
  61. {
  62. return false;
  63. }
  64. long chksum = 0;
  65. int count = size - 4;
  66. long check = -1;
  67. int i;
  68. for (i = offset; i < count; i += 4)
  69. {
  70. check = raw[i] & 0xff;
  71. check |= (raw[i + 1] << 8) & 0xff00;
  72. check |= (raw[i + 2] << 0x10) & 0xff0000;
  73. check |= (raw[i + 3] << 0x18) & 0xff000000;
  74. chksum ^= check;
  75. }
  76. check = raw[i] & 0xff;
  77. check |= (raw[i + 1] << 8) & 0xff00;
  78. check |= (raw[i + 2] << 0x10) & 0xff0000;
  79. check |= (raw[i + 3] << 0x18) & 0xff000000;
  80. return check == chksum;
  81. }
  82. /**
  83. * Equivalent to calling {@link #appendChecksum(byte[], int, int)} with parameters (raw, 0, raw.length)
  84. * @param raw data array to compute the checksum from
  85. */
  86. public static void appendChecksum(final byte[] raw)
  87. {
  88. NewCrypt.appendChecksum(raw, 0, raw.length);
  89. }
  90. /**
  91. * Method to append packet checksum at the end of the packet.
  92. * @param raw data array to compute the checksum from
  93. * @param offset offset where to start in the data array
  94. * @param size number of bytes to compute the checksum from
  95. */
  96. public static void appendChecksum(final byte[] raw, final int offset, final int size)
  97. {
  98. long chksum = 0;
  99. int count = size - 4;
  100. long ecx;
  101. int i;
  102. for (i = offset; i < count; i += 4)
  103. {
  104. ecx = raw[i] & 0xff;
  105. ecx |= (raw[i + 1] << 8) & 0xff00;
  106. ecx |= (raw[i + 2] << 0x10) & 0xff0000;
  107. ecx |= (raw[i + 3] << 0x18) & 0xff000000;
  108. chksum ^= ecx;
  109. }
  110. ecx = raw[i] & 0xff;
  111. ecx |= (raw[i + 1] << 8) & 0xff00;
  112. ecx |= (raw[i + 2] << 0x10) & 0xff0000;
  113. ecx |= (raw[i + 3] << 0x18) & 0xff000000;
  114. raw[i] = (byte) (chksum & 0xff);
  115. raw[i + 1] = (byte) ((chksum >> 0x08) & 0xff);
  116. raw[i + 2] = (byte) ((chksum >> 0x10) & 0xff);
  117. raw[i + 3] = (byte) ((chksum >> 0x18) & 0xff);
  118. }
  119. /**
  120. * Packet is first XOR encoded with <code>key</code> then, the last 4 bytes are overwritten with the the XOR "key".<br>
  121. * Thus this assume that there is enough room for the key to fit without overwriting data.
  122. * @param raw The raw bytes to be encrypted
  123. * @param key The 4 bytes (int) XOR key
  124. */
  125. public static void encXORPass(byte[] raw, int key)
  126. {
  127. NewCrypt.encXORPass(raw, 0, raw.length, key);
  128. }
  129. /**
  130. * Packet is first XOR encoded with <code>key</code> then, the last 4 bytes are overwritten with the the XOR "key".<br>
  131. * Thus this assume that there is enough room for the key to fit without overwriting data.
  132. * @param raw The raw bytes to be encrypted
  133. * @param offset The beginning of the data to be encrypted
  134. * @param size Length of the data to be encrypted
  135. * @param key The 4 bytes (int) XOR key
  136. */
  137. static void encXORPass(byte[] raw, final int offset, final int size, int key)
  138. {
  139. int stop = size - 8;
  140. int pos = 4 + offset;
  141. int edx;
  142. int ecx = key; // Initial xor key
  143. while (pos < stop)
  144. {
  145. edx = (raw[pos] & 0xFF);
  146. edx |= (raw[pos + 1] & 0xFF) << 8;
  147. edx |= (raw[pos + 2] & 0xFF) << 16;
  148. edx |= (raw[pos + 3] & 0xFF) << 24;
  149. ecx += edx;
  150. edx ^= ecx;
  151. raw[pos++] = (byte) (edx & 0xFF);
  152. raw[pos++] = (byte) ((edx >> 8) & 0xFF);
  153. raw[pos++] = (byte) ((edx >> 16) & 0xFF);
  154. raw[pos++] = (byte) ((edx >> 24) & 0xFF);
  155. }
  156. raw[pos++] = (byte) (ecx & 0xFF);
  157. raw[pos++] = (byte) ((ecx >> 8) & 0xFF);
  158. raw[pos++] = (byte) ((ecx >> 16) & 0xFF);
  159. raw[pos++] = (byte) ((ecx >> 24) & 0xFF);
  160. }
  161. /**
  162. * Method to decrypt using Blowfish-Blockcipher in ECB mode.<br>
  163. * The results will be directly placed inside {@code raw} array.<br>
  164. * This method does not do any error checking, since the calling code<br>
  165. * should ensure sizes.
  166. * @param raw the data array to be decrypted
  167. * @param offset the offset at which to start decrypting
  168. * @param size the number of bytes to be decrypted
  169. */
  170. public void decrypt(byte[] raw, final int offset, final int size)
  171. {
  172. for (int i = offset; i < (offset + size); i += 8)
  173. {
  174. _cipher.decryptBlock(raw, i);
  175. }
  176. }
  177. /**
  178. * Method to encrypt using Blowfish-Blockcipher in ECB mode.<br>
  179. * The results will be directly placed inside {@code raw} array.<br>
  180. * This method does not do any error checking, since the calling code should ensure sizes.
  181. * @param raw the data array to be decrypted
  182. * @param offset the offset at which to start decrypting
  183. * @param size the number of bytes to be decrypted
  184. */
  185. public void crypt(byte[] raw, final int offset, final int size)
  186. {
  187. for (int i = offset; i < (offset + size); i += 8)
  188. {
  189. _cipher.encryptBlock(raw, i);
  190. }
  191. }
  192. }