LoginCrypt.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 com.l2jserver.util.Rnd;
  18. /**
  19. * @author KenM
  20. */
  21. public class LoginCrypt
  22. {
  23. private static final byte[] STATIC_BLOWFISH_KEY =
  24. {
  25. (byte) 0x6b,
  26. (byte) 0x60,
  27. (byte) 0xcb,
  28. (byte) 0x5b,
  29. (byte) 0x82,
  30. (byte) 0xce,
  31. (byte) 0x90,
  32. (byte) 0xb1,
  33. (byte) 0xcc,
  34. (byte) 0x2b,
  35. (byte) 0x6c,
  36. (byte) 0x55,
  37. (byte) 0x6c,
  38. (byte) 0x6c,
  39. (byte) 0x6c,
  40. (byte) 0x6c
  41. };
  42. private static final NewCrypt _STATIC_CRYPT = new NewCrypt(STATIC_BLOWFISH_KEY);
  43. private NewCrypt _crypt = null;
  44. private boolean _static = true;
  45. /**
  46. * Method to initialize the the blowfish cipher with dynamic key.
  47. * @param key the blowfish key to initialize the dynamic blowfish cipher with
  48. */
  49. public void setKey(byte[] key)
  50. {
  51. _crypt = new NewCrypt(key);
  52. }
  53. /**
  54. * Method to decrypt an incoming login client packet.
  55. * @param raw array with encrypted data
  56. * @param offset offset where the encrypted data is located
  57. * @param size number of bytes of encrypted data
  58. * @return true when checksum could be verified, false otherwise
  59. * @throws IOException the size is not multiple of blowfishs block size or the raw array can't hold size bytes starting at offset due to it's size
  60. */
  61. public boolean decrypt(byte[] raw, final int offset, final int size) throws IOException
  62. {
  63. if ((size % 8) != 0)
  64. {
  65. throw new IOException("size have to be multiple of 8");
  66. }
  67. if ((offset + size) > raw.length)
  68. {
  69. throw new IOException("raw array too short for size starting from offset");
  70. }
  71. _crypt.decrypt(raw, offset, size);
  72. return NewCrypt.verifyChecksum(raw, offset, size);
  73. }
  74. /**
  75. * Method to encrypt an outgoing packet to login client.<br>
  76. * Performs padding and resizing of data array.
  77. * @param raw array with plain data
  78. * @param offset offset where the plain data is located
  79. * @param size number of bytes of plain data
  80. * @return the new array size
  81. * @throws IOException packet is too long to make padding and add verification data
  82. */
  83. public int encrypt(byte[] raw, final int offset, int size) throws IOException
  84. {
  85. // reserve checksum
  86. size += 4;
  87. if (_static)
  88. {
  89. // reserve for XOR "key"
  90. size += 4;
  91. // padding
  92. size += 8 - (size % 8);
  93. if ((offset + size) > raw.length)
  94. {
  95. throw new IOException("packet too long");
  96. }
  97. NewCrypt.encXORPass(raw, offset, size, Rnd.nextInt());
  98. _STATIC_CRYPT.crypt(raw, offset, size);
  99. _static = false;
  100. }
  101. else
  102. {
  103. // padding
  104. size += 8 - (size % 8);
  105. if ((offset + size) > raw.length)
  106. {
  107. throw new IOException("packet too long");
  108. }
  109. NewCrypt.appendChecksum(raw, offset, size);
  110. _crypt.crypt(raw, offset, size);
  111. }
  112. return size;
  113. }
  114. }