public static void createKeyPair(int length, String identity, char[] password, OutputStream privateKeyStream,
                                     OutputStream
                                         publicKeyStream) throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM, PROVIDER);
        SecureRandom random = SecureRandom.getInstanceStrong();
        keyPairGenerator.initialize(length, random);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        PGPPublicKey publicKey = new JcaPGPKeyConverter().getPGPPublicKey(PGPPublicKey.RSA_GENERAL, keyPair
            .getPublic(), new Date());
        RSAPrivateCrtKey privateCrtKey = (RSAPrivateCrtKey) keyPair.getPrivate();
        RSASecretBCPGKey secretBCPGKey = new RSASecretBCPGKey(privateCrtKey.getPrivateExponent(), privateCrtKey
            .getPrimeP(), privateCrtKey.getPrimeQ());
        PGPPrivateKey privateKey = new PGPPrivateKey(publicKey.getKeyID(), publicKey.getPublicKeyPacket(),
            secretBCPGKey);
        PGPKeyPair pgpKeyPair = new PGPKeyPair(publicKey, privateKey);
        PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder().build().get
            (HashAlgorithmTags.SHA1);
        PGPSecretKey secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, pgpKeyPair, identity,
            calculator, null, null, new JcaPGPContentSignerBuilder(pgpKeyPair.getPublicKey().getAlgorithm(),
            HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, calculator)
            .setProvider(PROVIDER).build(password));


        try(ArmoredOutputStream privateArm = new ArmoredOutputStream(privateKeyStream);
            ArmoredOutputStream publicArm = new ArmoredOutputStream(publicKeyStream)) {
            secretKey.encode(privateArm);
            secretKey.getPublicKey().encode(publicArm);
        }
    }
public static void createKeyPair ( int length , String identity , char [ ] password , OutputStream privateKeyStream , OutputStream publicKeyStream ) throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator . getInstance ( ALGORITHM , PROVIDER ) ; SecureRandom random = SecureRandom . getInstanceStrong ( ) ; keyPairGenerator . initialize ( length , random ) ; KeyPair keyPair = keyPairGenerator . generateKeyPair ( ) ; PGPPublicKey publicKey = new JcaPGPKeyConverter ( ) . getPGPPublicKey ( PGPPublicKey . RSA_GENERAL , keyPair . getPublic ( ) , new Date ( ) ) ; RSAPrivateCrtKey privateCrtKey = ( RSAPrivateCrtKey ) keyPair . getPrivate ( ) ; RSASecretBCPGKey secretBCPGKey = new RSASecretBCPGKey ( privateCrtKey . getPrivateExponent ( ) , privateCrtKey . getPrimeP ( ) , privateCrtKey . getPrimeQ ( ) ) ; PGPPrivateKey privateKey = new PGPPrivateKey ( publicKey . getKeyID ( ) , publicKey . getPublicKeyPacket ( ) , secretBCPGKey ) ; PGPKeyPair pgpKeyPair = new PGPKeyPair ( publicKey , privateKey ) ; PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder ( ) . build ( ) . get ( HashAlgorithmTags . SHA1 ) ; PGPSecretKey secretKey = new PGPSecretKey ( PGPSignature . DEFAULT_CERTIFICATION , pgpKeyPair , identity , calculator , null , null , new JcaPGPContentSignerBuilder ( pgpKeyPair . getPublicKey ( ) . getAlgorithm ( ) , HashAlgorithmTags . SHA1 ) , new JcePBESecretKeyEncryptorBuilder ( PGPEncryptedData . CAST5 , calculator ) . setProvider ( PROVIDER ) . build ( password ) ) ; try ( ArmoredOutputStream privateArm = new ArmoredOutputStream ( privateKeyStream ) ; ArmoredOutputStream publicArm = new ArmoredOutputStream ( publicKeyStream ) ) { secretKey . encode ( privateArm ) ; secretKey . getPublicKey ( ) . encode ( publicArm ) ; } }
Creates a private/public PGP key pair.
@param length length in bytes for the keys
@param identity name used for the keys
@param password passphrase used for the private key
@param privateKeyStream stream to receive the encoded private key
@param publicKeyStream stream to receive the encoded public key
@throws NoSuchProviderException if there is an error with the security provider
@throws NoSuchAlgorithmException is there is an error with the security provider
@throws PGPException if there is an error creating the keys
@throws IOException if there is an error writing to the streams