List of usage examples for java.security.spec RSAKeyGenParameterSpec RSAKeyGenParameterSpec
public RSAKeyGenParameterSpec(int keysize, BigInteger publicExponent)
From source file:MainClass.java
public static void main(String[] args) throws Exception { int eValue = 79; int bitLength = 1024; // KeySize BigInteger e = e = new BigInteger(Integer.toString(eValue)); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(bitLength);//from w ww . j ava 2s.co m KeyPair kp = kpg.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); e = new BigInteger(Integer.toString(eValue)); System.out.println("e =" + e); RSAKeyGenParameterSpec param = new RSAKeyGenParameterSpec(bitLength, e); kpg.initialize(param); kp = kpg.generateKeyPair(); kfactory = KeyFactory.getInstance("RSA", "SunRsaSign"); kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); }
From source file:net.link.util.test.pkix.PkiTestUtils.java
public static KeyPair generateKeyPair(String algorithm) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); SecureRandom random = new SecureRandom(); if ("RSA".equals(keyPairGenerator.getAlgorithm())) keyPairGenerator.initialize(new RSAKeyGenParameterSpec(RSA_KEYSIZE, RSAKeyGenParameterSpec.F4), random); else if (keyPairGenerator instanceof DSAKeyPairGenerator) { DSAKeyPairGenerator dsaKeyPairGenerator = (DSAKeyPairGenerator) keyPairGenerator; dsaKeyPairGenerator.initialize(DSA_MODLEN, false, random); }/*w ww. j a va2 s. c o m*/ return keyPairGenerator.generateKeyPair(); }
From source file:im.whistle.crypt.Crypt.java
/** * Generates a private/public key pair./* ww w . j a va 2 s . c om*/ * @param args Arguments, element at 0 is the key size * @param callback Callback */ public static void genkeys(JSONArray args, AsyncCallback<JSONArray> callback) { try { Log.i("whistle", "Generating key pair ..."); PRNGProvider.init(); // Ensure OpenSSL fix KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); int bits = args.getInt(0); int exp = args.getInt(1); keyPairGenerator.initialize(new RSAKeyGenParameterSpec(bits, BigInteger.valueOf(exp))); KeyPair keyPair = keyPairGenerator.genKeyPair(); String priv = "-----BEGIN RSA PRIVATE KEY-----\n" + Base64.encodeToString(keyPair.getPrivate().getEncoded(), Base64.DEFAULT).trim() + "\n-----END RSA PRIVATE KEY-----"; String pub = "-----BEGIN PUBLIC KEY-----\n" + Base64.encodeToString(keyPair.getPublic().getEncoded(), Base64.DEFAULT).trim() + "\n-----END PUBLIC KEY-----"; JSONArray res = new JSONArray(); res.put(priv); res.put(pub); callback.success(res); } catch (Exception ex) { Log.w("whistle", "Key pair generation failed: " + ex.getMessage()); callback.error(ex); } }
From source file:ru.jts_dev.authserver.config.KeyGenerationConfig.java
@Bean public KeyPairGenerator RSAKeyPairGenerator() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keygen;// ww w . ja v a2 s . co m keygen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4); keygen.initialize(spec); return keygen; }
From source file:test.be.fedict.eid.applet.RSATest.java
@Test public void testManualEncryption() throws Exception { while (true) { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME); SecureRandom random = new SecureRandom(); int keySize = 128; keyPairGenerator.initialize(new RSAKeyGenParameterSpec(keySize, RSAKeyGenParameterSpec.F0), random); KeyPair keyPair = keyPairGenerator.generateKeyPair(); PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey; LOG.debug("private key modulus: " + rsaPrivateKey.getModulus()); RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; LOG.debug("public key modulus: " + rsaPublicKey.getModulus()); LOG.debug("public key exponent: " + rsaPublicKey.getPublicExponent()); LOG.debug("modulus size: " + rsaPublicKey.getModulus().toByteArray().length); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); int dataSize = keySize / 8 - 11; byte[] data1 = new byte[dataSize]; for (int i = 0; i < data1.length; i++) { data1[i] = 0x00;//from w w w . j av a 2s . c o m } byte[] data2 = new byte[dataSize]; for (int i = 0; i < data2.length; i++) { data2[i] = 0x00; } data2[data2.length - 1] = 0x07; byte[] signatureValue1 = cipher.doFinal(data1); LOG.debug("signature size: " + signatureValue1.length); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] signatureValue2 = cipher.doFinal(data2); BigInteger sigBigInt1 = new BigInteger(signatureValue1); BigInteger sigBigInt2 = new BigInteger(signatureValue2); BigInteger msgBigInt1 = sigBigInt1.modPow(rsaPublicKey.getPublicExponent(), rsaPublicKey.getModulus()); BigInteger msgBigInt2 = sigBigInt2.modPow(rsaPublicKey.getPublicExponent(), rsaPublicKey.getModulus()); LOG.debug("msg big int: " + msgBigInt1); byte[] msgBytes1 = msgBigInt1.toByteArray(); LOG.debug("original message size: " + msgBytes1.length); LOG.debug("original message1: " + new String(Hex.encodeHex(msgBytes1))); LOG.debug("original message2: " + new String(Hex.encodeHex(msgBigInt2.toByteArray()))); LOG.debug("msg1 prime: " + msgBigInt1.isProbablePrime(100)); LOG.debug("msg2 prime: " + msgBigInt2.isProbablePrime(100)); // BigInteger.pow offers a very naive implementation LOG.debug("calculating s1^e..."); BigInteger s1_e = sigBigInt1.pow(rsaPublicKey.getPublicExponent().intValue()); LOG.debug("s1^e: " + s1_e); LOG.debug("calculating s2^e..."); BigInteger s2_e = sigBigInt2.pow(rsaPublicKey.getPublicExponent().intValue()); LOG.debug("s2^e: " + s2_e); LOG.debug("calculating GCD..."); LOG.debug("msg1: " + msgBigInt1); LOG.debug("msg2: " + msgBigInt2); BigInteger a = s1_e.subtract(msgBigInt1); BigInteger b = s2_e.subtract(msgBigInt2); LOG.debug("a: " + a); LOG.debug("b: " + b); BigInteger candidateModulus = a.gcd(b); LOG.debug("candidate modulus: " + candidateModulus); LOG.debug("candidate modulus size: " + candidateModulus.toByteArray().length); BigInteger s_e = s1_e.multiply(s2_e); BigInteger m = msgBigInt1.multiply(msgBigInt2); while (false == rsaPublicKey.getModulus().equals(candidateModulus)) { LOG.error("incorrect candidate modulus"); LOG.debug("modulus | candidate modulus: " + candidateModulus.remainder(rsaPublicKey.getModulus()).equals(BigInteger.ZERO)); s_e = s_e.multiply(s1_e); m = m.multiply(msgBigInt1); BigInteger n1 = s_e.subtract(m).gcd(a); BigInteger n2 = s_e.subtract(m).gcd(b); candidateModulus = n1.gcd(n2); // try / 2 LOG.debug("new modulus: " + n1); LOG.debug("new modulus: " + n2); LOG.debug("candidate modulus: " + candidateModulus); LOG.debug("actual mod: " + rsaPublicKey.getModulus()); } } }
From source file:org.demosoft.medieval.life.loginserver.LoginController.java
private LoginController() throws GeneralSecurityException { _log.info("Loading LoginContoller..."); _keyPairs = new ScrambledKeyPair[10]; KeyPairGenerator keygen = null; keygen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4); keygen.initialize(spec);//from w w w .j a v a 2 s . c om // generate the initial set of keys for (int i = 0; i < 10; i++) { _keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair()); } _log.info("Cached 10 KeyPairs for RSA communication"); testCipher((RSAPrivateKey) _keyPairs[0]._pair.getPrivate()); // Store keys for blowfish communication generateBlowFishKeys(); }
From source file:be.e_contract.mycarenet.common.SessionKey.java
/** * Generator constructor. Creates a new MyCareNet session key. * /*from www . j a v a 2 s. com*/ * @param keySize * the RSA key size. */ public SessionKey(int keySize) { KeyPairGenerator keyPairGenerator; try { keyPairGenerator = KeyPairGenerator.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("RSA algo not available", e); } SecureRandom random = new SecureRandom(); try { keyPairGenerator.initialize(new RSAKeyGenParameterSpec(keySize, RSAKeyGenParameterSpec.F4), random); } catch (InvalidAlgorithmParameterException e) { throw new RuntimeException("unsupported key size: " + keySize); } this.keyPair = keyPairGenerator.generateKeyPair(); }
From source file:com.l2jfree.loginserver.manager.GameServerManager.java
/** * Load RSA keys//from www . j av a 2 s.c om * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException */ private void loadRSAKeys() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(512, RSAKeyGenParameterSpec.F4); keyGen.initialize(spec); _keyPairs = new KeyPair[KEYS_SIZE]; for (int i = 0; i < KEYS_SIZE; i++) { _keyPairs[i] = keyGen.genKeyPair(); } _log.info("GameServerManager: Cached " + _keyPairs.length + " RSA keys for Game Server communication."); }
From source file:test.integ.be.fedict.trust.util.TestUtils.java
public static KeyPair generateKeyPair(String algorithm) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); SecureRandom random = new SecureRandom(); if ("RSA".equals(keyPairGenerator.getAlgorithm())) { keyPairGenerator.initialize(new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4), random); } else if (keyPairGenerator instanceof DSAKeyPairGenerator) { DSAKeyPairGenerator dsaKeyPairGenerator = (DSAKeyPairGenerator) keyPairGenerator; dsaKeyPairGenerator.initialize(512, false, random); }/*from w w w . j a va 2 s.c om*/ return keyPairGenerator.generateKeyPair(); }
From source file:org.cogroo.addon.util.SecurityUtil.java
public KeyPair genKeyPair() { KeyPair kpr = null;// ww w .ja va2 s . co m try { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(new RSAKeyGenParameterSpec(RSAKEYSIZE, RSAKeyGenParameterSpec.F4)); kpr = kpg.generateKeyPair(); } catch (NoSuchAlgorithmException e) { LOG.log(Level.SEVERE, "Error generating key pair", e); } catch (InvalidAlgorithmParameterException e) { LOG.log(Level.SEVERE, "Error generating key pair", e); } return kpr; }