PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/loginserver/src/loginserver/network/ncrypt/KeyGen.java

http://aionknight.googlecode.com/
Java | 102 lines | 38 code | 16 blank | 48 comment | 1 complexity | 5a0d03f22f4bc05c103a505cdfc9c6dc MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0
  1. /*
  2. * Emulator game server Aion 2.7 from the command of developers 'Aion-Knight Dev. Team' is
  3. * free software; you can redistribute it and/or modify it under the terms of
  4. * GNU affero general Public License (GNU GPL)as published by the free software
  5. * security (FSF), or to License version 3 or (at your option) any later
  6. * version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT ANY
  9. * WARRANTY; without even the implied warranties related to
  10. * CONSUMER PROPERTIES and SUITABILITY FOR CERTAIN PURPOSES. For details, see
  11. * General Public License is the GNU.
  12. *
  13. * You should have received a copy of the GNU affero general Public License along with this program.
  14. * If it is not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. * Cambridge, MA 02139, USA
  16. *
  17. * Web developers : http://aion-knight.ru
  18. * Support of the game client : Aion 2.7- 'Arena of Death' (Innova)
  19. * The version of the server : Aion-Knight 2.7 (Beta version)
  20. */
  21. package loginserver.network.ncrypt;
  22. import java.security.GeneralSecurityException;
  23. import java.security.KeyPairGenerator;
  24. import java.security.spec.RSAKeyGenParameterSpec;
  25. import javax.crypto.Cipher;
  26. import javax.crypto.KeyGenerator;
  27. import javax.crypto.SecretKey;
  28. import org.apache.log4j.Logger;
  29. import commons.utils.Rnd;
  30. /**
  31. * Key generator. It generates keys or keyPairs for Blowfish and RSA
  32. */
  33. public class KeyGen
  34. {
  35. /**
  36. * Logger for this class.
  37. */
  38. private static final Logger log = Logger.getLogger(KeyGen.class);
  39. /**
  40. * Key generator for blowfish
  41. */
  42. private static KeyGenerator blowfishKeyGen;
  43. /**
  44. * Public/Static RSA KeyPairs with encrypted modulus N
  45. */
  46. private static EncryptedRSAKeyPair[] encryptedRSAKeyPairs;
  47. /**
  48. * Initialize Key Generator (Blowfish keygen and RSA keygen)
  49. *
  50. * @throws GeneralSecurityException
  51. */
  52. public static void init() throws GeneralSecurityException
  53. {
  54. log.info("Initializing Key Generator...");
  55. blowfishKeyGen = KeyGenerator.getInstance("Blowfish");
  56. KeyPairGenerator rsaKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
  57. RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
  58. rsaKeyPairGenerator.initialize(spec);
  59. encryptedRSAKeyPairs = new EncryptedRSAKeyPair[10];
  60. for (int i = 0; i < 10; i++)
  61. {
  62. encryptedRSAKeyPairs[i] = new EncryptedRSAKeyPair(rsaKeyPairGenerator.generateKeyPair());
  63. }
  64. // Pre-init RSA cipher.. saving about 300ms
  65. Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
  66. rsaCipher.init(Cipher.DECRYPT_MODE, encryptedRSAKeyPairs[0].getRSAKeyPair().getPrivate());
  67. }
  68. /**
  69. * Generate and return blowfish key
  70. *
  71. * @return Random generated blowfish key
  72. */
  73. public static SecretKey generateBlowfishKey()
  74. {
  75. return blowfishKeyGen.generateKey();
  76. }
  77. /**
  78. * Get common RSA Public/Static Key Pair with encrypted modulus N
  79. *
  80. * @return encryptedRSAkeypairs
  81. */
  82. public static EncryptedRSAKeyPair getEncryptedRSAKeyPair()
  83. {
  84. return encryptedRSAKeyPairs[Rnd.nextInt(10)];
  85. }
  86. }