NewCrypt.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. import java.io.IOException;
  17. import java.util.logging.Logger;
  18. /**
  19. * This class ...
  20. *
  21. * @version $Revision: 1.3.4.1 $ $Date: 2005/03/27 15:30:09 $
  22. */
  23. public class NewCrypt
  24. {
  25. protected static Logger _log = Logger.getLogger(NewCrypt.class.getName());
  26. BlowfishEngine _crypt;
  27. BlowfishEngine _decrypt;
  28. /**
  29. * @param blowfishKey
  30. */
  31. public NewCrypt(byte[] blowfishKey)
  32. {
  33. _crypt = new BlowfishEngine();
  34. _crypt.init(true, blowfishKey);
  35. _decrypt = new BlowfishEngine();
  36. _decrypt.init(false, blowfishKey);
  37. }
  38. public NewCrypt(String key)
  39. {
  40. this(key.getBytes());
  41. }
  42. public static boolean verifyChecksum(byte[] raw)
  43. {
  44. return NewCrypt.verifyChecksum(raw, 0, raw.length);
  45. }
  46. public static boolean verifyChecksum(byte[] raw, final int offset, final int size)
  47. {
  48. // check if size is multiple of 4 and if there is more then only the checksum
  49. if ((size & 3) != 0 || size <= 4)
  50. {
  51. return false;
  52. }
  53. long chksum = 0;
  54. int count = size-4;
  55. long check = -1;
  56. int i;
  57. for (i = offset; i<count; i+=4)
  58. {
  59. check = raw[i] &0xff;
  60. check |= raw[i+1] << 8 &0xff00;
  61. check |= raw[i+2] << 0x10 &0xff0000;
  62. check |= raw[i+3] << 0x18 &0xff000000;
  63. chksum ^= check;
  64. }
  65. check = raw[i] &0xff;
  66. check |= raw[i+1] << 8 &0xff00;
  67. check |= raw[i+2] << 0x10 &0xff0000;
  68. check |= raw[i+3] << 0x18 &0xff000000;
  69. return check == chksum;
  70. }
  71. public static void appendChecksum(byte[] raw)
  72. {
  73. NewCrypt.appendChecksum(raw, 0, raw.length);
  74. }
  75. public static void appendChecksum(byte[] raw, final int offset, final int size)
  76. {
  77. long chksum = 0;
  78. int count = size-4;
  79. long ecx;
  80. int i;
  81. for (i = offset; i<count; i+=4)
  82. {
  83. ecx = raw[i] &0xff;
  84. ecx |= raw[i+1] << 8 &0xff00;
  85. ecx |= raw[i+2] << 0x10 &0xff0000;
  86. ecx |= raw[i+3] << 0x18 &0xff000000;
  87. chksum ^= ecx;
  88. }
  89. ecx = raw[i] &0xff;
  90. ecx |= raw[i+1] << 8 &0xff00;
  91. ecx |= raw[i+2] << 0x10 &0xff0000;
  92. ecx |= raw[i+3] << 0x18 &0xff000000;
  93. raw[i] = (byte) (chksum &0xff);
  94. raw[i+1] = (byte) (chksum >>0x08 &0xff);
  95. raw[i+2] = (byte) (chksum >>0x10 &0xff);
  96. raw[i+3] = (byte) (chksum >>0x18 &0xff);
  97. }
  98. /**
  99. * Packet is first XOR encoded with <code>key</code>
  100. * Then, the last 4 bytes are overwritten with the the XOR "key".
  101. * Thus this assume that there is enough room for the key to fit without overwriting data.
  102. * @param raw The raw bytes to be encrypted
  103. * @param key The 4 bytes (int) XOR key
  104. */
  105. public static void encXORPass(byte[] raw, int key)
  106. {
  107. NewCrypt.encXORPass(raw, 0, raw.length, key);
  108. }
  109. /**
  110. * Packet is first XOR encoded with <code>key</code>
  111. * Then, the last 4 bytes are overwritten with the the XOR "key".
  112. * Thus this assume that there is enough room for the key to fit without overwriting data.
  113. * @param raw The raw bytes to be encrypted
  114. * @param offset The begining of the data to be encrypted
  115. * @param size Length of the data to be encrypted
  116. * @param key The 4 bytes (int) XOR key
  117. */
  118. public static void encXORPass(byte[] raw, final int offset, final int size, int key)
  119. {
  120. int stop = size-8;
  121. int pos = 4 + offset;
  122. int edx;
  123. int ecx = key; // Initial xor key
  124. while (pos < stop)
  125. {
  126. edx = (raw[pos] & 0xFF);
  127. edx |= (raw[pos+1] & 0xFF) << 8;
  128. edx |= (raw[pos+2] & 0xFF) << 16;
  129. edx |= (raw[pos+3] & 0xFF) << 24;
  130. ecx += edx;
  131. edx ^= ecx;
  132. raw[pos++] = (byte) (edx & 0xFF);
  133. raw[pos++] = (byte) (edx >> 8 & 0xFF);
  134. raw[pos++] = (byte) (edx >> 16 & 0xFF);
  135. raw[pos++] = (byte) (edx >> 24 & 0xFF);
  136. }
  137. raw[pos++] = (byte) (ecx & 0xFF);
  138. raw[pos++] = (byte) (ecx >> 8 & 0xFF);
  139. raw[pos++] = (byte) (ecx >> 16 & 0xFF);
  140. raw[pos++] = (byte) (ecx >> 24 & 0xFF);
  141. }
  142. public byte[] decrypt(byte[] raw) throws IOException
  143. {
  144. byte[] result = new byte[raw.length];
  145. int count = raw.length /8;
  146. for (int i=0; i<count;i++)
  147. {
  148. _decrypt.processBlock(raw,i*8,result,i*8);
  149. }
  150. return result;
  151. }
  152. public void decrypt(byte[] raw, final int offset, final int size) throws IOException
  153. {
  154. byte[] result = new byte[size];
  155. int count = size /8;
  156. for (int i=0; i<count;i++)
  157. {
  158. _decrypt.processBlock(raw,offset + i*8,result,i*8);
  159. }
  160. // TODO can the crypt and decrypt go direct to the array
  161. System.arraycopy(result, 0, raw, offset, size);
  162. }
  163. public byte[] crypt(byte[] raw) throws IOException
  164. {
  165. int count = raw.length /8;
  166. byte[] result = new byte[raw.length];
  167. for (int i=0; i<count;i++)
  168. {
  169. _crypt.processBlock(raw,i*8,result,i*8);
  170. }
  171. return result;
  172. }
  173. public void crypt(byte[] raw, final int offset, final int size) throws IOException
  174. {
  175. int count = size/8;
  176. byte[] result = new byte[size];
  177. for (int i=0; i<count;i++)
  178. {
  179. _crypt.processBlock(raw,offset + i*8,result,i*8);
  180. }
  181. // TODO can the crypt and decrypt go direct to the array
  182. System.arraycopy(result, 0, raw, offset, size);
  183. }
  184. }