NewCrypt.java 6.8 KB

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