BlowFishKey.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.communityserver.network.readpackets;
  16. import java.security.GeneralSecurityException;
  17. import java.security.interfaces.RSAPrivateKey;
  18. import javax.crypto.Cipher;
  19. import com.l2jserver.communityserver.network.GameServerThread;
  20. import com.l2jserver.communityserver.network.netcon.BaseReadPacket;
  21. import com.l2jserver.communityserver.network.netcon.crypt.NewCrypt;
  22. /**
  23. * @author -Wooden-
  24. */
  25. public class BlowFishKey extends BaseReadPacket
  26. {
  27. private final RSAPrivateKey _key;
  28. private final GameServerThread _gst;
  29. public BlowFishKey(final byte[] data, final RSAPrivateKey key, final GameServerThread gst)
  30. {
  31. super(data);
  32. _key = key;
  33. _gst = gst;
  34. final int size = readD();
  35. final byte[] tempKey = readB(size);
  36. try
  37. {
  38. final Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
  39. rsaCipher.init(Cipher.DECRYPT_MODE, _key);
  40. final byte[] tempDecryptKey = rsaCipher.doFinal(tempKey);
  41. // there are nulls before the key we must remove them
  42. int i = 0;
  43. for (; i < tempDecryptKey.length; i++)
  44. {
  45. if (tempDecryptKey[i] != 0)
  46. break;
  47. }
  48. final byte key2[] = new byte[tempDecryptKey.length-i];
  49. System.arraycopy(tempDecryptKey, i, key2, 0, tempDecryptKey.length - i);
  50. _gst.setCrypt(new NewCrypt(key2));
  51. }
  52. catch (GeneralSecurityException e)
  53. {
  54. e.printStackTrace();
  55. }
  56. }
  57. @Override
  58. public final void run()
  59. {
  60. // nothing
  61. }
  62. }