BlowFishKey.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.loginserver.gameserverpackets;
  16. import java.security.GeneralSecurityException;
  17. import java.security.interfaces.RSAPrivateKey;
  18. import java.util.logging.Logger;
  19. import javax.crypto.Cipher;
  20. import com.l2jserver.util.network.BaseRecievePacket;
  21. /**
  22. * @author -Wooden-
  23. *
  24. */
  25. public class BlowFishKey extends BaseRecievePacket
  26. {
  27. byte[] _key;
  28. protected static final Logger _log = Logger.getLogger(BlowFishKey.class.getName());
  29. /**
  30. * @param decrypt
  31. */
  32. public BlowFishKey(byte[] decrypt, RSAPrivateKey privateKey)
  33. {
  34. super(decrypt);
  35. int size = readD();
  36. byte[] tempKey = readB(size);
  37. try
  38. {
  39. byte [] tempDecryptKey;
  40. Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
  41. rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);
  42. tempDecryptKey = rsaCipher.doFinal(tempKey);
  43. // there are nulls before the key we must remove them
  44. int i = 0;
  45. int len = tempDecryptKey.length;
  46. for(; i < len; i++)
  47. {
  48. if(tempDecryptKey[i] != 0)
  49. break;
  50. }
  51. _key = new byte[len-i];
  52. System.arraycopy(tempDecryptKey,i,_key,0,len-i);
  53. }
  54. catch(GeneralSecurityException e)
  55. {
  56. _log.severe("Error While decrypting blowfish key (RSA)");
  57. e.printStackTrace();
  58. }
  59. /*catch(IOException ioe)
  60. {
  61. //TODO: manage
  62. }*/
  63. }
  64. public byte[] getKey()
  65. {
  66. return _key;
  67. }
  68. }