Crypt.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver;
  20. import java.nio.ByteBuffer;
  21. /**
  22. * This class ...
  23. *
  24. * @version $Revision: 1.3.4.3 $ $Date: 2005/03/27 15:29:18 $
  25. */
  26. public class Crypt
  27. {
  28. private final byte[] _key = new byte[16];
  29. private boolean _isEnabled;
  30. public void setKey(byte[] key)
  31. {
  32. System.arraycopy(key,0, _key, 0, key.length);
  33. _isEnabled = true;
  34. }
  35. public void decrypt(ByteBuffer buf)
  36. {
  37. if (!_isEnabled)
  38. return;
  39. final int sz = buf.remaining();
  40. int temp = 0;
  41. for (int i = 0; i < sz; i++)
  42. {
  43. int temp2 = buf.get(i);
  44. buf.put(i, (byte)(temp2 ^ _key[i&15] ^ temp));
  45. temp = temp2;
  46. }
  47. int old = _key[8] &0xff;
  48. old |= _key[9] << 8 &0xff00;
  49. old |= _key[10] << 0x10 &0xff0000;
  50. old |= _key[11] << 0x18 &0xff000000;
  51. old += sz;
  52. _key[8] = (byte)(old &0xff);
  53. _key[9] = (byte)(old >> 0x08 &0xff);
  54. _key[10] = (byte)(old >> 0x10 &0xff);
  55. _key[11] = (byte)(old >> 0x18 &0xff);
  56. }
  57. public void encrypt(ByteBuffer buf)
  58. {
  59. if (!_isEnabled)
  60. return;
  61. int temp = 0;
  62. final int sz = buf.remaining();
  63. for (int i = 0; i < sz; i++)
  64. {
  65. int temp2 = buf.get(i);
  66. temp = temp2 ^ _key[i&15] ^ temp;
  67. buf.put(i, (byte) temp);
  68. }
  69. int old = _key[8] &0xff;
  70. old |= _key[9] << 8 &0xff00;
  71. old |= _key[10] << 0x10 &0xff0000;
  72. old |= _key[11] << 0x18 &0xff000000;
  73. old += sz;
  74. _key[8] = (byte)(old &0xff);
  75. _key[9] = (byte)(old >> 0x08 &0xff);
  76. _key[10] = (byte)(old >> 0x10 &0xff);
  77. _key[11] = (byte)(old >> 0x18 &0xff);
  78. }
  79. }