PGPEncryptionUtil { public static boolean verifyClearSign( byte[] message, PGPPublicKeyRing pgpRings ) throws IOException, PGPException, SignatureException { ArmoredInputStream aIn = new ArmoredInputStream( new ByteArrayInputStream( message ) ); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream lineOut = new ByteArrayOutputStream(); boolean isFirstLineClearText = aIn.isClearText(); int lookAhead = readInputLine( lineOut, aIn ); if ( lookAhead != -1 && isFirstLineClearText ) { bout.write( lineOut.toByteArray() ); while ( lookAhead != -1 && aIn.isClearText() ) { lookAhead = readInputLine( lineOut, lookAhead, aIn ); bout.write( lineOut.toByteArray() ); } } JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory( aIn ); PGPSignatureList p3 = ( PGPSignatureList ) pgpFact.nextObject(); PGPSignature sig = p3.get( 0 ); PGPPublicKey publicKey = pgpRings.getPublicKey( sig.getKeyID() ); sig.init( new JcaPGPContentVerifierBuilderProvider().setProvider( "BC" ), publicKey ); InputStream sigIn = new ByteArrayInputStream( bout.toByteArray() ); lookAhead = readInputLine( lineOut, sigIn ); processLine( sig, lineOut.toByteArray() ); if ( lookAhead != -1 ) { do { lookAhead = readInputLine( lineOut, lookAhead, sigIn ); sig.update( ( byte ) '\r' ); sig.update( ( byte ) '\n' ); processLine( sig, lineOut.toByteArray() ); } while ( lookAhead != -1 ); } sigIn.close(); return sig.verify(); } 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 testVerifyClearSign() throws Exception { InputStream secondPublicStream = findFile( PLUGIN_PUBLIC_KEY ); PGPPublicKeyRingCollection secondPublicKeyRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( secondPublicStream ), new JcaKeyFingerprintCalculator() ); PGPPublicKeyRing pgpKeyring = secondPublicKeyRingCollection .getPublicKeyRing( secondPublicKeyRingCollection.iterator().next().getPublicKey().getKeyID() ); String signedMessage = IOUtils.toString( findFile( "signedMessage.txt" ) ); logger.info( "\n" + signedMessage ); boolean result = PGPEncryptionUtil.verifyClearSign( signedMessage.getBytes(), pgpKeyring ); if ( result ) { logger.info( "signature verified." ); } else { logger.info( "signature verification failed." ); } assertEquals( true, result ); }