Search in sources :

Example 1 with JcaPGPObjectFactory

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);
    }
}
Also used : InputStream(java.io.InputStream) JcePublicKeyDataDecryptorFactoryBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder) PGPEncryptedDataList(org.bouncycastle.openpgp.PGPEncryptedDataList) IOException(java.io.IOException) PGPException(org.bouncycastle.openpgp.PGPException) Iterator(java.util.Iterator) PGPSecretKeyRingCollection(org.bouncycastle.openpgp.PGPSecretKeyRingCollection) BcKeyFingerprintCalculator(org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator) PGPPublicKeyEncryptedData(org.bouncycastle.openpgp.PGPPublicKeyEncryptedData) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey) JcaPGPObjectFactory(org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory)

Example 2 with JcaPGPObjectFactory

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;
}
Also used : PGPEncryptedDataList(org.bouncycastle.openpgp.PGPEncryptedDataList) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) JcaPGPObjectFactory(org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory)

Aggregations

PGPEncryptedDataList (org.bouncycastle.openpgp.PGPEncryptedDataList)2 JcaPGPObjectFactory (org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Iterator (java.util.Iterator)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1 PGPException (org.bouncycastle.openpgp.PGPException)1 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)1 PGPPublicKeyEncryptedData (org.bouncycastle.openpgp.PGPPublicKeyEncryptedData)1 PGPSecretKeyRingCollection (org.bouncycastle.openpgp.PGPSecretKeyRingCollection)1 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)1 JcePublicKeyDataDecryptorFactoryBuilder (org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder)1