PageRenderTime 89ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://aionxemu.googlecode.com/
Java | 96 lines | 33 code | 17 blank | 46 comment | 1 complexity | f7227e3ae4e47894f49e7f82113b14c1 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-2-Clause
  1. /**
  2. * This file is part of Aion X Emu <aionxemu.com>
  3. *
  4. * This is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser Public License
  15. * along with this software. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package loginserver.network.ncrypt;
  18. import com.aionemu.commons.utils.Rnd;
  19. import org.apache.log4j.Logger;
  20. import javax.crypto.Cipher;
  21. import javax.crypto.KeyGenerator;
  22. import javax.crypto.SecretKey;
  23. import java.security.GeneralSecurityException;
  24. import java.security.KeyPairGenerator;
  25. import java.security.spec.RSAKeyGenParameterSpec;
  26. /**
  27. * Key generator. It generates keys or keyPairs for Blowfish and RSA
  28. *
  29. * @author -Nemesiss-
  30. */
  31. public class KeyGen {
  32. /**
  33. * Logger for this class.
  34. */
  35. protected static final Logger log = Logger.getLogger(KeyGen.class);
  36. /**
  37. * Key generator for blowfish
  38. */
  39. private static KeyGenerator blowfishKeyGen;
  40. /**
  41. * Public/Static RSA KeyPairs with encrypted modulus N
  42. */
  43. private static EncryptedRSAKeyPair[] encryptedRSAKeyPairs;
  44. /**
  45. * Initialize Key Generator (Blowfish keygen and RSA keygen)
  46. *
  47. * @throws GeneralSecurityException
  48. */
  49. public static void init() throws GeneralSecurityException {
  50. log.info("Initializing Key Generator...");
  51. blowfishKeyGen = KeyGenerator.getInstance("Blowfish");
  52. KeyPairGenerator rsaKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
  53. RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
  54. rsaKeyPairGenerator.initialize(spec);
  55. encryptedRSAKeyPairs = new EncryptedRSAKeyPair[10];
  56. for (int i = 0; i < 10; i++) {
  57. encryptedRSAKeyPairs[i] = new EncryptedRSAKeyPair(rsaKeyPairGenerator.generateKeyPair());
  58. }
  59. // Pre-init RSA cipher.. saving about 300ms
  60. Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
  61. rsaCipher.init(Cipher.DECRYPT_MODE, encryptedRSAKeyPairs[0].getRSAKeyPair().getPrivate());
  62. }
  63. /**
  64. * Generate and return blowfish key
  65. *
  66. * @return Random generated blowfish key
  67. */
  68. public static SecretKey generateBlowfishKey() {
  69. return blowfishKeyGen.generateKey();
  70. }
  71. /**
  72. * Get common RSA Public/Static Key Pair with encrypted modulus N
  73. *
  74. * @return encryptedRSAkeypairs
  75. */
  76. public static EncryptedRSAKeyPair getEncryptedRSAKeyPair() {
  77. return encryptedRSAKeyPairs[Rnd.nextInt(10)];
  78. }
  79. }