@ SuppressWarnings ( "rawtypes" ) public static void decrypt ( InputStream encryptedStream , OutputStream targetStream , InputStream privateKeyStream , char [ ] password ) throws Exception { BcKeyFingerprintCalculator calculator = new BcKeyFingerprintCalculator ( ) ; PGPObjectFactory factory = new PGPObjectFactory ( PGPUtil . getDecoderStream ( encryptedStream ) , calculator ) ; PGPEncryptedDataList dataList ; Object object = factory . nextObject ( ) ; if ( object instanceof PGPEncryptedDataList ) { dataList = ( PGPEncryptedDataList ) object ; } else { dataList = ( PGPEncryptedDataList ) factory . nextObject ( ) ; } Iterator objects = dataList . getEncryptedDataObjects ( ) ; PGPPrivateKey privateKey = null ; PGPPublicKeyEncryptedData data = null ; while ( privateKey == null && objects . hasNext ( ) ) { data = ( PGPPublicKeyEncryptedData ) objects . next ( ) ; privateKey = findSecretKey ( privateKeyStream , data . getKeyID ( ) , password ) ; } if ( privateKey == null ) { throw new IllegalArgumentException ( "Secret key for message not found." ) ; } decryptData ( privateKey , data , calculator , targetStream ) ; }
Performs decryption of a given stream using a PGP private key .