/** * Generates an RSA key pair with a 2048-bit private key and F4 (65537) as the public exponent. * @return the key pair. */ public static KeyPair generateRSAKeyPair() throws GeneralSecurityException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec keyGenSpec = new RSAKeyGenParameterSpec( DEFAULT_RSA_KEY_SIZE_BITS, DEFAULT_RSA_PUB_EXPONENT); keyGen.initialize(keyGenSpec, PRNG); return keyGen.generateKeyPair(); }
/** * Generate a spec */ private static RSAKeyGenParameterSpec genSpec(int size, BigInteger exp) { return new RSAKeyGenParameterSpec(size, exp); }
keyPairGenerator.initialize(new RSAKeyGenParameterSpec(keySize, publicExponent));
RSAKeyGenParameterSpec keyGenParameterSpec = new RSAKeyGenParameterSpec(1408, RSAKeyGenParameterSpec.F0); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keyGenParameterSpec); //.... rest of the code will be the same I posted above
/** * Generates an RSA key pair. * * @param bits The amount of bits * @return The key pair */ public static KeyPair generate(int bits) throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(bits, RSAKeyGenParameterSpec.F4); keygen.initialize(spec); return keygen.generateKeyPair(); }
public KeyPair genKeyPair() { KeyPair kpr = null; 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; }
public KeyPair genKeyPair() { KeyPair kpr = null; 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; }
public CertRASession(String emailPrivate, String phonePrivate) throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4); SecureRandom secureRandom = new SecureRandom(); secureRandom.setSeed(System.currentTimeMillis()); keyPairGenerator.initialize(params, secureRandom); KeyPair keyPair = keyPairGenerator.genKeyPair(); this.privateKey = keyPair.getPrivate(); this.publicKey = keyPair.getPublic(); this.emailPrivate = emailPrivate; this.phonePrivate = phonePrivate; }
KeyPair keys = null; try { RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(spec); keys = keyGen.generateKeyPair(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } if(keys != null){ PublicKey mPublicKey = (PublicKey) keys.getPublic(); PrivateKey mPrivateKey = (PrivateKey) keys.getPrivate(); }
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); // 17 is another often used value, beware to use proper RSA padding if you set it to 3 RSAKeyGenParameterSpec kpgSpec = new RSAKeyGenParameterSpec(2048, BigInteger.valueOf(3)); kpg.initialize(kpgSpec); KeyPair kp = kpg.genKeyPair();
@Override public AlgorithmParameterSpec getAlgorithmSpec() { return new RSAKeyGenParameterSpec(length.getLength(), RSAKeyGenParameterSpec.F4); } }
/** * Generator constructor. Creates a new MyCareNet session key. * * @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(); }
/** * Generates an asymmetric RSA key pair of the specified key length. * * @param keyLength The length the RSA keys should have. * @param securityProvider the security provider (e.g. bouncycastle) * @return An asymmetric RSA key pair of the specified length. */ public static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider) { try { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", securityProvider); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(keyLength.value(), RSA_PUBLIC_EXP); gen.initialize(params, new SecureRandom()); return gen.generateKeyPair(); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) { logger.error("Exception while generation of RSA key pair of length {}:", keyLength, e); } return null; }
@Test public void testKeyConversion() throws GeneralSecurityException { /* Generate random key pair */ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); AlgorithmParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4); keyPairGenerator.initialize(spec, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); /* Encode private key as PKCS#8 base64 string */ byte[] privKeyBytes = keyPair.getPrivate().getEncoded(); String privKeyStr = DatatypeConverter.printBase64Binary(privKeyBytes); /* Decode private key as PKCS#8 base64 string */ byte[] privKeyBytes2 = DatatypeConverter.parseBase64Binary(privKeyStr); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privKeyBytes2); PrivateKey privateKey = keyFactory.generatePrivate(privSpec); /* Ensure key is the same */ byte[] privKeyBytes3 = privateKey.getEncoded(); assertEquals( DatatypeConverter.printHexBinary(privKeyBytes), DatatypeConverter.printHexBinary(privKeyBytes3)); }
public static KeyPair generateRSAKeypair(int keysize, BigInteger publicExponent, SecureRandom random) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { BigInteger tmpPublicExponent = publicExponent; if (tmpPublicExponent == null) { tmpPublicExponent = RSAKeyGenParameterSpec.F4; } AlgorithmParameterSpec params = new RSAKeyGenParameterSpec(keysize, tmpPublicExponent); KeyPairGenerator kpGen = getKeyPairGenerator("RSA"); synchronized (kpGen) { if (random == null) { kpGen.initialize(params); } else { kpGen.initialize(params, random); } return kpGen.generateKeyPair(); } }
public static KeyPair generateRSAKeypair(final int keysize, final BigInteger publicExponent, final SecureRandom random) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { BigInteger tmpPublicExponent = publicExponent; if (tmpPublicExponent == null) { tmpPublicExponent = RSAKeyGenParameterSpec.F4; } AlgorithmParameterSpec params = new RSAKeyGenParameterSpec(keysize, tmpPublicExponent); KeyPairGenerator kpGen = getKeyPairGenerator("RSA"); synchronized (kpGen) { if (random == null) { kpGen.initialize(params); } else { kpGen.initialize(params, random); } return kpGen.generateKeyPair(); } }
/** * Initialize Key Generator (Blowfish keygen and RSA keygen) * * @throws GeneralSecurityException */ public static void init() throws GeneralSecurityException { System.out.println(String.format("Initializing Key Generator...")); blowfishKeyGen = KeyGenerator.getInstance("Blowfish"); KeyPairGenerator rsaKeyPairGenerator = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4); rsaKeyPairGenerator.initialize(spec); encryptedRSAKeyPairs = new EncryptedRSAKeyPair[10]; for (int i = 0; i < 10; i++) { encryptedRSAKeyPairs[i] = new EncryptedRSAKeyPair(rsaKeyPairGenerator.generateKeyPair()); } // Pre-init RSA cipher.. saving about 300ms Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding"); rsaCipher.init(Cipher.DECRYPT_MODE, encryptedRSAKeyPairs[0].getRSAKeyPair().getPrivate()); }
private static KeyPair generateKeyPair(PublicKey pubKey) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { String keyAlg = pubKey.getAlgorithm(); AlgorithmParameterSpec params = null; if ("RSA".equals(keyAlg)) { RSAPublicKey rsaKey = (RSAPublicKey) pubKey; params = new RSAKeyGenParameterSpec(rsaKey.getModulus().bitLength(), rsaKey.getPublicExponent()); } else if ("EC".equals(keyAlg)) { ECPublicKey ecKey = (ECPublicKey) pubKey; params = ecKey.getParams(); } else if ("DSA".equals(keyAlg)) { DSAPublicKey dsaKey = (DSAPublicKey) pubKey; DSAParams p = dsaKey.getParams(); params = new DSAParameterSpec(p.getP(), p.getQ(), p.getG()); } else { throw new UnsupportedOperationException("No support for " + keyAlg); } KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlg); keyGen.initialize(params); return keyGen.generateKeyPair(); }
private void encryptIdentityKeyBiometric(byte[] encKey) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore"); keyPairGenerator.initialize(new KeyGenParameterSpec.Builder( "quickPass", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT ) .setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(2048, F4)) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA384, KeyProperties.DIGEST_SHA512) .setUserAuthenticationRequired(true) .build()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); //or try with "RSA" cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic()); this.biometricKeyEncrypted = cipher.doFinal(encKey); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } } }
@TargetApi(Build.VERSION_CODES.M) static void createKeysM(String alias, boolean requireAuth) { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE); keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setAlgorithmParameterSpec( new RSAKeyGenParameterSpec(1024, F4)) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA384, KeyProperties.DIGEST_SHA512) // Only permit the private key to be used if the user authenticated // within the last five minutes. .setUserAuthenticationRequired(requireAuth) .build()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } }