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