PGPKeyHelper { public static PGPPublicKey readPublicKey( InputStream is ) throws IOException, PGPException { PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() ); Iterator keyRingIter = pgpPub.getKeyRings(); while ( keyRingIter.hasNext() ) { PGPPublicKeyRing keyRing = ( PGPPublicKeyRing ) keyRingIter.next(); Iterator keyIter = keyRing.getPublicKeys(); while ( keyIter.hasNext() ) { PGPPublicKey key = ( PGPPublicKey ) keyIter.next(); if ( key.isEncryptionKey() ) { return key; } } } throw new IllegalArgumentException( "Can't find encryption key in key ring." ); } private  PGPKeyHelper(); static PGPPublicKey readPublicKey( InputStream is ); static PGPPublicKey readPublicKey( String filePath ); static PGPPublicKey readPublicKeyFromString( String key ); static PGPPrivateKey readPrivateKey( InputStream is, String password ); static PGPPrivateKey readPrivateKey( String filePath, String password ); static String getFingerprint( PGPPublicKey publicKey ); static String getOwnerString( PGPPublicKey publicKey );  }
@Test public void testReadPublicKeyFromStream() throws IOException, PGPException { try ( InputStream is = new FileInputStream( PGPTestDataFactory.PUBLIC_KEY_PATH ) ) { PGPPublicKey publicKey = PGPKeyHelper.readPublicKey( is ); checkPublicKey( publicKey ); } }
@Test public void testReadPublicKeyFromPath() throws IOException, PGPException { PGPPublicKey publicKey = PGPKeyHelper.readPublicKey( PGPTestDataFactory.PUBLIC_KEY_PATH ); checkPublicKey( publicKey ); }