2
0

NewCrypt.java 6.9 KB

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