PGPEncryptionUtil { public static KeyPair generateKeyPair( String userId, String secretPwd, boolean armored ) throws PGPException { try { KeyPair keyPair = new KeyPair(); PGPKeyRingGenerator krgen = generateKeyRingGenerator( userId, secretPwd, keyPair ); PGPPublicKeyRing pkr = krgen.generatePublicKeyRing(); ByteArrayOutputStream pubOut = new ByteArrayOutputStream(); pkr.encode( pubOut ); pubOut.close(); PGPSecretKeyRing skr = krgen.generateSecretKeyRing(); ByteArrayOutputStream secOut = new ByteArrayOutputStream(); skr.encode( secOut ); secOut.close(); keyPair.setPubKeyring( armored ? armorByteArray( pubOut.toByteArray() ) : pubOut.toByteArray() ); keyPair.setSecKeyring( armored ? armorByteArray( secOut.toByteArray() ) : secOut.toByteArray() ); return keyPair; } catch ( Exception e ) { throw new PGPException( "Error in generateKeyPair", e ); } } 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 testGenerateKeyPair() throws Exception { KeyPair keyPair = PGPEncryptionUtil.generateKeyPair( USER_ID, SECRET_PWD, false ); assertNotNull( PGPEncryptionUtil .findPublicKeyById( new ByteArrayInputStream( keyPair.getPubKeyring() ), keyPair.getSubKeyId() ) ); assertNotNull( PGPEncryptionUtil .findSecretKeyByFingerprint( new ByteArrayInputStream( keyPair.getSecKeyring() ), keyPair.getPrimaryKeyFingerprint() ) ); }
@Test public void testKeySigning() throws PGPException, IOException { KeyPair first = PGPEncryptionUtil.generateKeyPair( "first@key.com", "first", false ); KeyPair second = PGPEncryptionUtil.generateKeyPair( "second@key.com", "second", false ); signKeyAndPrintIds( first, second, "second" ); InputStream firstPublicStream = new ByteArrayInputStream( first.getPubKeyring() ); InputStream secondPublicStream = new ByteArrayInputStream( second.getPubKeyring() ); PGPPublicKeyRingCollection firstPublicKeyRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( firstPublicStream ), new JcaKeyFingerprintCalculator() ); PGPPublicKeyRingCollection secondPublicKeyRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( secondPublicStream ), new JcaKeyFingerprintCalculator() ); if ( firstPublicKeyRingCollection.getKeyRings().hasNext() ) { PGPPublicKeyRing firstPublicKeyRing = null; PGPPublicKeyRing secondPublicKeyRing = null; firstPublicKeyRing = firstPublicKeyRingCollection.getKeyRings().next(); secondPublicKeyRing = secondPublicKeyRingCollection.getKeyRings().next(); assertEquals( true, printPublicKeySignatures( firstPublicKeyRing.getPublicKey(), secondPublicKeyRing.getPublicKey() ) ); } }