public InputStream decryptFile ( InputStream inputStream , InputStream keyIn , String passPhrase ) throws IOException { try { PGPEncryptedDataList enc = getPGPEncryptedDataList ( inputStream ) ; Iterator it = enc . getEncryptedDataObjects ( ) ; PGPPrivateKey sKey = null ; PGPPublicKeyEncryptedData pbe = null ; PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection ( PGPUtil . getDecoderStream ( keyIn ) , new BcKeyFingerprintCalculator ( ) ) ; while ( sKey == null && it . hasNext ( ) ) { pbe = ( PGPPublicKeyEncryptedData ) it . next ( ) ; sKey = findSecretKey ( pgpSec , pbe . getKeyID ( ) , passPhrase ) ; } if ( sKey == null ) { throw new IllegalArgumentException ( "secret key for message not found." ) ; } InputStream clear = pbe . getDataStream ( new JcePublicKeyDataDecryptorFactoryBuilder ( ) . setProvider ( BouncyCastleProvider . PROVIDER_NAME ) . build ( sKey ) ) ; JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory ( clear ) ; return new LazyMaterializeDecryptorInputStream ( pgpFact ) ; } catch ( PGPException e ) { throw new IOException ( e ) ; } }
Taking in a file inputstream keyring inputstream and a passPhrase generate a decrypted file inputstream .