PGPEncryptionUtil { public static X509Certificate getX509CertificateFromPgpKeyPair( PGPPublicKey pgpPublicKey, PGPSecretKey pgpSecretKey, String secretPwd, String issuer, String subject, Date dateOfIssue, Date dateOfExpiry, BigInteger serial ) throws PGPException, CertificateException, IOException { JcaPGPKeyConverter c = new JcaPGPKeyConverter(); PublicKey publicKey = c.getPublicKey( pgpPublicKey ); PrivateKey privateKey = c.getPrivateKey( pgpSecretKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) ) ); X509v3CertificateBuilder certBuilder = new X509v3CertificateBuilder( new X500Name( issuer ), serial, dateOfIssue, dateOfExpiry, new X500Name( subject ), SubjectPublicKeyInfo.getInstance( publicKey.getEncoded() ) ); byte[] certBytes = certBuilder.build( new JCESigner( privateKey, "SHA256withRSA" ) ).getEncoded(); CertificateFactory certificateFactory = CertificateFactory.getInstance( "X.509" ); return ( X509Certificate ) certificateFactory.generateCertificate( new ByteArrayInputStream( certBytes ) ); } private  PGPEncryptionUtil(); static byte[] encrypt( final byte[] message, final PGPPublicKey publicKey, boolean armored ); static byte[] decrypt( final byte[] encryptedMessage, final PGPSecretKeyRing secretKeyRing,
                                  final String secretPwd ); static byte[] decrypt( final byte[] encryptedMessage, final InputStream secretKeyRing,
                                  final String secretPwd ); static boolean verifySignature( ContentAndSignatures contentAndSignatures, PGPPublicKey publicKey ); static ContentAndSignatures decryptAndReturnSignatures( final byte[] encryptedMessage,
                                                                   final PGPSecretKeyRing secretKeyRing,
                                                                   final String secretPwd ); static byte[] decryptAndVerify( byte[] encryptedMessage, final PGPSecretKey secretKey,
                                           final String secretPwd, final PGPPublicKey publicKey ); static byte[] signAndEncrypt( final byte[] message, final PGPSecretKey secretKey, final String secretPwd,
                                         final PGPPublicKey publicKey, final boolean armored ); static KeyPair generateKeyPair( String userId, String secretPwd, boolean armored ); static String BytesToHex( byte[] bytes ); static byte[] armorByteArray( byte[] data ); static boolean verify( byte[] signedMessage, PGPPublicKey publicKey ); static byte[] sign( byte[] message, PGPSecretKey secretKey, String secretPwd, boolean armor ); static byte[] extractContentFromClearSign( byte[] signedMessage ); static boolean verifyClearSign( byte[] message, PGPPublicKeyRing pgpRings ); static byte[] clearSign( byte[] message, PGPSecretKey pgpSecKey, char[] pass, String digestName ); static PGPPublicKey findPublicKeyById( InputStream publicKeyRing, String keyId ); static PGPPublicKey findPublicKeyByFingerprint( InputStream publicKeyRing, String fingerprint ); static PGPSecretKey findSecretKeyById( InputStream secretKeyRing, String keyId ); static PGPSecretKey findSecretKeyByFingerprint( InputStream secretKeyRing, String fingerprint ); static X509Certificate getX509CertificateFromPgpKeyPair( PGPPublicKey pgpPublicKey,
                                                                    PGPSecretKey pgpSecretKey, String secretPwd,
                                                                    String issuer, String subject, Date dateOfIssue,
                                                                    Date dateOfExpiry, BigInteger serial ); static PGPPrivateKey getPrivateKey( final PGPSecretKey secretKey, final String secretPwd ); static String armorByteArrayToString( byte[] data ); static PGPPublicKeyRing signPublicKey( PGPPublicKeyRing publicKeyRing, String id, PGPSecretKey secretKey,
                                                  String secretKeyPassword ); static boolean verifyPublicKey( PGPPublicKey keyToVerify, String id, PGPPublicKey keyToVerifyWith ); static PGPPublicKeyRing removeSignature( PGPPublicKeyRing keyToRemoveFrom, String id ); static PGPPublicKeyRing removeSignature( PGPPublicKeyRing keyToRemoveFrom,
                                                    PGPPublicKey keySignatureToRemove );  }
@Test public void testGetX509CertificateFromPgpKeyPair() throws Exception { Date today = new Date(); PGPPublicKey pgpPublicKey = PGPEncryptionUtil.findPublicKeyById( findFile( PUBLIC_KEYRING ), PUBLIC_KEY_ID ); PGPSecretKey pgpSecretKey = PGPEncryptionUtil.findSecretKeyById( findFile( SECRET_KEYRING ), SECRET_KEY_ID ); X509Certificate x509Certificate = PGPEncryptionUtil .getX509CertificateFromPgpKeyPair( pgpPublicKey, pgpSecretKey, SECRET_PWD, "C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting cc," + " OU=Certification Services Division," + " CN=Thawte Server CA/emailAddress=server-certs@thawte.com", "C=US, ST=Maryland, L=Pasadena, O=Brent Baccala," + "OU=FreeSoft, CN=www.freesoft.org/emailAddress=baccala@freesoft.org", today, new Date( today.getTime() + ( 1000 * 60 * 60 * 24 ) ), new BigInteger( "1" ) ); assertNotNull( x509Certificate ); JcaPGPKeyConverter c = new JcaPGPKeyConverter(); PublicKey publicKey = c.getPublicKey( pgpSecretKey.getPublicKey() ); x509Certificate.verify( publicKey, new BouncyCastleProvider() ); }