Example usage for org.bouncycastle.openpgp.bc BcPGPObjectFactory nextObject

List of usage examples for org.bouncycastle.openpgp.bc BcPGPObjectFactory nextObject

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp.bc BcPGPObjectFactory nextObject.

Prototype

public Object nextObject() throws IOException 

Source Link

Document

Return the next object in the stream, or null if the end of stream is reached.

Usage

From source file:org.pgptool.gui.encryption.implpgp.EncryptionServicePgpImpl.java

License:Open Source License

/**
 * decrypt the passed in message stream.
 * //w w  w.  j a  v a  2  s  .  co  m
 * Inspired by
 * https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/examples/KeyBasedFileProcessor.java
 * 
 * @param countingStream
 *            this stream is passed for progress reporting only, must not be
 *            used to actually read data
 */
private void decryptStream(PGPPublicKeyEncryptedData pbe, PGPPrivateKey privateKey, OutputStream outputStream,
        Updater optionalProgress, CountingInputStream countingStream)
        throws UserRequestedCancellationException {
    try {
        InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey));

        BcPGPObjectFactory plainFact = new BcPGPObjectFactory(clear);
        Object message = plainFact.nextObject();
        if (message instanceof PGPMarker) {
            message = plainFact.nextObject();
        }

        BcPGPObjectFactory pgpFactory = null;
        if (message instanceof PGPCompressedData) {
            PGPCompressedData cData = (PGPCompressedData) message;
            pgpFactory = new BcPGPObjectFactory(cData.getDataStream());
            message = pgpFactory.nextObject();
        }

        int watchDog = 0;
        while (message != null) {
            Preconditions.checkState(watchDog++ < 100, "Inifinite loop watch dog just hit");

            if (message instanceof PGPLiteralData) {
                PGPLiteralData ld = (PGPLiteralData) message;

                // NOTE: We know initial file name (in case we need it):
                // ld.getFileName();
                InputStream unc = ld.getInputStream();
                OutputStream fOut = new BufferedOutputStream(outputStream);
                if (optionalProgress != null) {
                    optionalProgress.updateStepInfo("progress.decrypting");
                }

                pipeStream(unc, fOut, BUFFER_SIZE, optionalProgress, countingStream);
                fOut.close();
                unc.close();

                if (pbe.isIntegrityProtected()) {
                    if (!pbe.verify()) {
                        throw new RuntimeException("message failed integrity check");
                    }
                }
                return;
            } else if (message instanceof PGPOnePassSignatureList) {
                log.info("PGPOnePassSignatureList is not implemented yet. Skipping signature validation");
                // NOTE: Here is a place to copyright from
                // http://stackoverflow.com/questions/19173181/bouncycastle-pgp-decrypt-and-verify
                Preconditions.checkArgument(pgpFactory != null,
                        "File format is not supported. pgpFact is supposed to be initialized by that time");
                message = pgpFactory.nextObject();
            } else if (message instanceof PGPSignatureList) {
                log.info("PGPSignatureList is not implemented yet. Skipping signature validation");
                Preconditions.checkArgument(pgpFactory != null,
                        "File format is not supported. pgpFact is supposed to be initialized by that time");
                message = pgpFactory.nextObject();
            } else {
                throw new PGPException(
                        "Don't know how to decrypt the input file. Encountered unexpected block: " + message);
            }
        }
    } catch (Throwable e) {
        Throwables.throwIfInstanceOf(e, UserRequestedCancellationException.class);
        throw new RuntimeException("Decryption failed", e);
    }
}

From source file:org.pgptool.gui.encryption.implpgp.EncryptionServicePgpImpl.java

License:Open Source License

private String getInitialFileName(PGPPublicKeyEncryptedData pbe, PGPPrivateKey privateKey) {
    InputStream clear = null;/*from   w  ww .  j a va 2s  . co m*/
    try {
        clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey));

        BcPGPObjectFactory plainFact = new BcPGPObjectFactory(clear);
        Object message = plainFact.nextObject();
        if (message instanceof PGPMarker) {
            message = plainFact.nextObject();
        }

        BcPGPObjectFactory pgpFactory = null;
        if (message instanceof PGPCompressedData) {
            PGPCompressedData cData = (PGPCompressedData) message;
            pgpFactory = new BcPGPObjectFactory(cData.getDataStream());
            message = pgpFactory.nextObject();
        }

        int watchDog = 0;
        while (message != null) {
            Preconditions.checkState(watchDog++ < 100, "Inifinite loop watch dog just hit");
            if (message instanceof PGPLiteralData) {
                PGPLiteralData ld = (PGPLiteralData) message;
                return ld.getFileName();
            } else if (message instanceof PGPOnePassSignatureList) {
                Preconditions.checkState(pgpFactory != null, "pgpFactory supposed to be not null");
                message = pgpFactory.nextObject();
            } else if (message instanceof PGPSignatureList) {
                Preconditions.checkState(pgpFactory != null, "pgpFactory supposed to be not null");
                message = pgpFactory.nextObject();
            } else {
                throw new PGPException(
                        "Don't know how to decrypt the input file. Encountered unexpected block: " + message);
            }
        }
        throw new IllegalStateException("Unknown file format, cannot determine initial file name");
    } catch (Throwable e) {
        throw new RuntimeException("Failed to get initial file name", e);
    } finally {
        IoStreamUtils.safeClose(clear);
    }
}