Example usage for org.bouncycastle.openpgp PGPEncryptedDataList get

List of usage examples for org.bouncycastle.openpgp PGPEncryptedDataList get

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPEncryptedDataList get.

Prototype

public Object get(int index) 

Source Link

Document

Gets the encryption method object at the specified index.

Usage

From source file:org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.java

License:Apache License

private InputStream getDecryptedData(Exchange exchange, InputStream encryptedStream)
        throws Exception, PGPException {
    PGPObjectFactory pgpFactory = new PGPObjectFactory(encryptedStream);
    Object firstObject = pgpFactory.nextObject();
    // the first object might be a PGP marker packet 
    PGPEncryptedDataList enc = getEcryptedDataList(pgpFactory, firstObject);

    if (enc == null) {
        throw getFormatException();
    }//from   ww  w . j  ava2s  . c om
    PGPPublicKeyEncryptedData pbe = null;
    PGPPrivateKey key = null;
    // find encrypted data for which a private key exists in the secret key ring
    for (int i = 0; i < enc.size() && key == null; i++) {
        Object encryptedData = enc.get(i);
        if (!(encryptedData instanceof PGPPublicKeyEncryptedData)) {
            throw getFormatException();
        }
        pbe = (PGPPublicKeyEncryptedData) encryptedData;
        key = secretKeyAccessor.getPrivateKey(exchange, pbe.getKeyID());
        if (key != null) {
            // take the first key
            break;
        }
    }
    if (key == null) {
        throw new PGPException(
                "PGP message is encrypted with a key which could not be found in the Secret Keyring.");
    }

    InputStream encData = pbe
            .getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider(getProvider()).build(key));
    return encData;
}

From source file:org.apache.gobblin.crypto.GPGFileDecryptor.java

License:Apache License

/**
 * Taking in a file inputstream and a passPhrase, generate a decrypted file inputstream.
 * @param inputStream file inputstream/*  w  w w  .  j av  a 2s .com*/
 * @param passPhrase passPhrase
 * @return
 * @throws IOException
 */
public InputStream decryptFile(InputStream inputStream, String passPhrase) throws IOException {

    PGPEncryptedDataList enc = getPGPEncryptedDataList(inputStream);
    PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
    InputStream clear;

    try {
        clear = pbe
                .getDataStream(new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder()
                        .setProvider(BouncyCastleProvider.PROVIDER_NAME).build())
                                .setProvider(BouncyCastleProvider.PROVIDER_NAME)
                                .build(passPhrase.toCharArray()));

        JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear);

        return new LazyMaterializeDecryptorInputStream(pgpFact);
    } catch (PGPException e) {
        throw new IOException(e);
    }
}

From source file:org.brownsocks.payments.gateways.enets.pgp.BCPGPProvider.java

@Override
public String decryptAndVerify(String messageIn) throws IOException, SignatureVerificationException {

    try {//from   w w  w . ja  v  a  2  s .  c o m
        /* Stage zero: Convert to ASCII armored format and open a decoding stream */
        InputStream is = new ByteArrayInputStream(Base64.decode(messageIn));
        InputStream decoderStream = PGPUtil.getDecoderStream(is);

        /* Stage one: Init a decrypting stream */
        PGPObjectFactory pgpFactory = new PGPObjectFactory(decoderStream);
        PGPEncryptedDataList cryptedDataList = (PGPEncryptedDataList) pgpFactory.nextObject();
        PGPPublicKeyEncryptedData cryptedData = (PGPPublicKeyEncryptedData) cryptedDataList.get(0);
        InputStream clearStream = cryptedData.getDataStream(getCryptingPrivateKey(), _provider);

        /* Stage two: Seperate the XML data from the signatures */
        PGPObjectFactory plainFact = new PGPObjectFactory(clearStream);

        PGPOnePassSignatureList onePassSignatureList = (PGPOnePassSignatureList) plainFact.nextObject();
        PGPLiteralData literalData = (PGPLiteralData) plainFact.nextObject();

        String xmlMessage = IOUtils.toString(literalData.getInputStream());
        PGPSignatureList signatureList = (PGPSignatureList) plainFact.nextObject();

        /* Stage three: Verify signature */
        PGPOnePassSignature ops = onePassSignatureList.get(0);
        PGPPublicKey key = _remotePublicKeyRing.getPublicKey(ops.getKeyID());
        ops.initVerify(key, _provider);
        ops.update(xmlMessage.getBytes());

        if (!ops.verify(signatureList.get(0))) {
            throw new SignatureVerificationException(
                    "Failed to verify message signature. Message authenticity cannot be thrusted.");
        }

        return xmlMessage;

    } catch (PGPException pgpException) {
        throw new IOException("PGP subsystem problem.", pgpException);

    } catch (SignatureException signException) {
        throw new IOException("PGP subsystem problem.", signException);

    } catch (Throwable t) {
        throw new IOException("Unknown error occured in PGP subsystem: " + t.getMessage(), t);

    }

}

From source file:uk.ac.ebi.ega.cipher.GPGStream.java

License:Apache License

public GPGStream(InputStream in, OutputStream out, int blocksize, char[] password, boolean mod,
        int pw_strength) {

    InputStream in_ = null;//from   w w w. jav a2s  .c o m
    {
        InputStream clear = null;
        try {
            if (mod)
                return;
            in_ = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
            PGPObjectFactory pgpF = new PGPObjectFactory(in_);
            PGPEncryptedDataList enc;
            Object o = pgpF.nextObject();
            if (o instanceof PGPEncryptedDataList) {
                enc = (PGPEncryptedDataList) o;
            } else {
                enc = (PGPEncryptedDataList) pgpF.nextObject();
            }
            PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
            clear = pbe.getDataStream(password, "BC");
            PGPObjectFactory pgpFact = new PGPObjectFactory(clear);
            PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject();
            pgpFact = new PGPObjectFactory(cData.getDataStream());
            PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();
            InputStream in__ = ld.getInputStream();
        } catch (PGPException ex) {
            Logger.getLogger(GPGStream.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchProviderException ex) {
            Logger.getLogger(GPGStream.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GPGStream.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Throwable t) {

        } finally {
            try {
                in_.close();
            } catch (IOException ex) {
                Logger.getLogger(GPGStream.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                clear.close();
            } catch (IOException ex) {
                Logger.getLogger(GPGStream.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}

From source file:uk.ac.ebi.ega.cipher.GPGStream.java

License:Apache License

public static InputStream getDecodingGPGInoutStream(FileInputStream fis, char[] pw)
        throws IOException, PGPException, NoSuchProviderException {
    InputStream in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(fis);

    PGPObjectFactory pgpF = new PGPObjectFactory(in);
    PGPEncryptedDataList enc;

    Object o = pgpF.nextObject();
    if (o instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) o;//from   w w w  . j a  v a2s.c  om
    } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
    }

    PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
    InputStream clear = pbe.getDataStream(pw, "BC");
    PGPObjectFactory pgpFact = new PGPObjectFactory(clear);
    PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject();
    pgpFact = new PGPObjectFactory(cData.getDataStream());
    PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();

    return ld.getInputStream();
}