use of org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory in project incubator-gobblin by apache.
the class GPGFileDecryptor method decryptFile.
/**
* Taking in a file inputstream, keyring inputstream and a passPhrase, generate a decrypted file inputstream.
* @param inputStream file inputstream
* @param keyIn keyring inputstream. This InputStream is owned by the caller.
* @param passPhrase passPhrase
* @return an {@link InputStream} for the decrypted content
* @throws IOException
*/
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);
}
}
use of org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory in project incubator-gobblin by apache.
the class GPGFileDecryptor method getPGPEncryptedDataList.
/**
* Generate a PGPEncryptedDataList from an inputstream
* @param inputStream file inputstream that needs to be decrypted
* @throws IOException
*/
private PGPEncryptedDataList getPGPEncryptedDataList(InputStream inputStream) throws IOException {
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
inputStream = PGPUtil.getDecoderStream(inputStream);
JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(inputStream);
PGPEncryptedDataList enc;
Object pgpfObject = pgpF.nextObject();
if (pgpfObject instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList) pgpfObject;
} else {
enc = (PGPEncryptedDataList) pgpF.nextObject();
}
return enc;
}
Aggregations