NewCrypt.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * <br>
  19. * Static methods are present to append/check the checksum of<br>
  20. * packets exchanged between the following partners:<br>
  21. * Login Server <-> Game Client<br>
  22. * Login Server <-> Game Server<br>
  23. * <br>
  24. * Also a static method is provided for the initial xor encryption<br>
  25. * between Login Server <-> Game Client.
  26. */
  27. public final class NewCrypt
  28. {
  29. private final BlowfishEngine _cipher;
  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)}<br>
  41. * with parameters (raw, 0, raw.length)
  42. * @param raw data array to be verified
  43. * @return true when the checksum of the data is valid, false otherwise
  44. * @see #verifyChecksum(byte[], int, int)
  45. */
  46. public static boolean verifyChecksum(final byte[] raw)
  47. {
  48. return NewCrypt.verifyChecksum(raw, 0, raw.length);
  49. }
  50. /**
  51. * Method to verify the checksum of a packet received by<br>
  52. * login server from game client.<br>
  53. * <br>
  54. * This is also used for game server <-> login server communication.
  55. * @param raw data array to be verified
  56. * @param offset at which offset to start verifying
  57. * @param size number of bytes to verify
  58. * @return true if the checksum of the data is valid, false otherwise
  59. */
  60. public static boolean verifyChecksum(final byte[] raw, final int offset, final int size)
  61. {
  62. // check if size is multiple of 4 and if there is more then only the checksum
  63. if (((size & 3) != 0) || (size <= 4))
  64. {
  65. return false;
  66. }
  67. long chksum = 0;
  68. int count = size - 4;
  69. long check = -1;
  70. int i;
  71. for (i = offset; i < count; i += 4)
  72. {
  73. check = raw[i] & 0xff;
  74. check |= (raw[i + 1] << 8) & 0xff00;
  75. check |= (raw[i + 2] << 0x10) & 0xff0000;
  76. check |= (raw[i + 3] << 0x18) & 0xff000000;
  77. chksum ^= check;
  78. }
  79. check = raw[i] & 0xff;
  80. check |= (raw[i + 1] << 8) & 0xff00;
  81. check |= (raw[i + 2] << 0x10) & 0xff0000;
  82. check |= (raw[i + 3] << 0x18) & 0xff000000;
  83. return check == chksum;
  84. }
  85. /**
  86. * Equivalent to calling {@link #appendChecksum(byte[], int, int)}<br>
  87. * with parameters (raw, 0, raw.length)
  88. * @param raw data array to compute the checksum from
  89. */
  90. public static void appendChecksum(final byte[] raw)
  91. {
  92. NewCrypt.appendChecksum(raw, 0, raw.length);
  93. }
  94. /**
  95. * Method to append packet checksum at the end of the packet.
  96. * @param raw data array to compute the checksum from
  97. * @param offset offset where to start in the data array
  98. * @param size number of bytes to compute the checksum from
  99. */
  100. public static void appendChecksum(final byte[] raw, final int offset, final int size)
  101. {
  102. long chksum = 0;
  103. int count = size - 4;
  104. long ecx;
  105. int i;
  106. for (i = offset; i < count; i += 4)
  107. {
  108. ecx = raw[i] & 0xff;
  109. ecx |= (raw[i + 1] << 8) & 0xff00;
  110. ecx |= (raw[i + 2] << 0x10) & 0xff0000;
  111. ecx |= (raw[i + 3] << 0x18) & 0xff000000;
  112. chksum ^= ecx;
  113. }
  114. ecx = raw[i] & 0xff;
  115. ecx |= (raw[i + 1] << 8) & 0xff00;
  116. ecx |= (raw[i + 2] << 0x10) & 0xff0000;
  117. ecx |= (raw[i + 3] << 0x18) & 0xff000000;
  118. raw[i] = (byte) (chksum & 0xff);
  119. raw[i + 1] = (byte) ((chksum >> 0x08) & 0xff);
  120. raw[i + 2] = (byte) ((chksum >> 0x10) & 0xff);
  121. raw[i + 3] = (byte) ((chksum >> 0x18) & 0xff);
  122. }
  123. /**
  124. * Packet is first XOR encoded with <code>key</code> Then, the last 4 bytes are overwritten with the the XOR "key". Thus this assume that there is enough room for the key to fit without overwriting data.
  125. * @param raw The raw bytes to be encrypted
  126. * @param key The 4 bytes (int) XOR key
  127. */
  128. public static void encXORPass(final byte[] raw, final int key)
  129. {
  130. NewCrypt.encXORPass(raw, 0, raw.length, key);
  131. }
  132. /**
  133. * Packet is first XOR encoded with <code>key</code> Then, the last 4 bytes are overwritten with the the XOR "key". Thus this assume that there is enough room for the key to fit without overwriting data.
  134. * @param raw The raw bytes to be encrypted
  135. * @param offset The beginning of the data to be encrypted
  136. * @param size Length of the data to be encrypted
  137. * @param key The 4 bytes (int) XOR key
  138. */
  139. public static void encXORPass(final byte[] raw, final int offset, final int size, int key)
  140. {
  141. int stop = size - 8;
  142. int pos = 4 + offset;
  143. int edx;
  144. int ecx = key; // Initial xor key
  145. while (pos < stop)
  146. {
  147. edx = (raw[pos] & 0xFF);
  148. edx |= (raw[pos + 1] & 0xFF) << 8;
  149. edx |= (raw[pos + 2] & 0xFF) << 16;
  150. edx |= (raw[pos + 3] & 0xFF) << 24;
  151. ecx += edx;
  152. edx ^= ecx;
  153. raw[pos++] = (byte) (edx & 0xFF);
  154. raw[pos++] = (byte) ((edx >> 8) & 0xFF);
  155. raw[pos++] = (byte) ((edx >> 16) & 0xFF);
  156. raw[pos++] = (byte) ((edx >> 24) & 0xFF);
  157. }
  158. raw[pos++] = (byte) (ecx & 0xFF);
  159. raw[pos++] = (byte) ((ecx >> 8) & 0xFF);
  160. raw[pos++] = (byte) ((ecx >> 16) & 0xFF);
  161. raw[pos++] = (byte) ((ecx >> 24) & 0xFF);
  162. }
  163. /**
  164. * Method to decrypt using Blowfish-Blockcipher in ECB mode. The results<br>
  165. * will be directly placed inside {@code raw} array.<br>
  166. * <br>
  167. * This method does not do any error checking, since the calling code<br>
  168. * should ensure sizes.
  169. * @param raw the data array to be decrypted
  170. * @param offset the offset at which to start decrypting
  171. * @param size the number of bytes to be decrypted
  172. */
  173. public void decrypt(byte[] raw, final int offset, final int size)
  174. {
  175. for (int i = offset; i < (offset + size); i += 8)
  176. {
  177. _cipher.decryptBlock(raw, i);
  178. }
  179. }
  180. /**
  181. * Method to encrypt using Blowfish-Blockcipher in ECB mode. The results<br>
  182. * will be directly placed inside {@code raw} array.<br>
  183. * <br>
  184. * This method does not do any error checking, since the calling code<br>
  185. * should ensure sizes.
  186. * @param raw the data array to be decrypted
  187. * @param offset the offset at which to start decrypting
  188. * @param size the number of bytes to be decrypted
  189. */
  190. public void crypt(byte[] raw, final int offset, final int size)
  191. {
  192. for (int i = offset; i < (offset + size); i += 8)
  193. {
  194. _cipher.encryptBlock(raw, i);
  195. }
  196. }
  197. }