Example usage for org.bouncycastle.openpgp PGPPBEEncryptedData isIntegrityProtected

List of usage examples for org.bouncycastle.openpgp PGPPBEEncryptedData isIntegrityProtected

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPPBEEncryptedData isIntegrityProtected.

Prototype

public boolean isIntegrityProtected() 

Source Link

Document

Checks whether the packet is integrity protected.

Usage

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * Decrypt the specified (PBE) input file
 *//*  w  w w .  j a  v a  2s .  c  o  m*/
public void decryptPBEBasedFile(String outputFilename, InputStream in, char[] passPhrase, boolean mdcRequired)
        throws PGPException {
    try {
        //
        // we need to be able to reset the stream if we try a
        // wrong passphrase, we'll assume that all the mechanisms
        // appear in the first 10k for the moment...
        //
        int READ_LIMIT = 10 * 1024;

        in.mark(READ_LIMIT);

        PGPPBEEncryptedData pbe;
        InputStream clear;
        int count = 0;

        for (;;) {
            InputStream dIn = PGPUtil.getDecoderStream(in);

            PGPObjectFactory pgpF = new PGPObjectFactory(dIn);
            PGPEncryptedDataList enc;
            Object o = pgpF.nextObject();

            //
            // the first object might be a PGP marker packet.
            //
            if (o instanceof PGPEncryptedDataList) {
                enc = (PGPEncryptedDataList) o;
            } else {
                enc = (PGPEncryptedDataList) pgpF.nextObject();
            }

            while (count < enc.size()) {
                if (enc.get(count) instanceof PGPPBEEncryptedData) {
                    break;
                }

                count++;
            }

            if (count >= enc.size()) {
                throw new PGPException("Passphrase invalid");
            }

            pbe = (PGPPBEEncryptedData) enc.get(count);

            try {
                clear = pbe.getDataStream(passPhrase, "BC");
            } catch (PGPKeyValidationException e) {
                in.reset();
                continue;
            }

            break;
        }

        PGPObjectFactory pgpFact = new PGPObjectFactory(clear);

        PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject();

        pgpFact = new PGPObjectFactory(cData.getDataStream());

        PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();

        if (outputFilename == null) {
            outputFilename = ld.getFileName();
        }

        FileOutputStream fOut = new FileOutputStream(outputFilename);

        InputStream unc = ld.getInputStream();

        int ch;
        while ((ch = unc.read()) >= 0) {
            fOut.write(ch);
        }

        if (pbe.isIntegrityProtected()) {
            if (!pbe.verify()) {
                throw new PGPException("Message failed integrity check");
            }
            if (_verbose) {
                System.out.println("Message integrity check passed");
            }
        } else {
            if (_verbose) {
                System.out.println("No message integrity check");
            }

            if (mdcRequired) {
                throw new PGPException("Missing required message integrity check");
            }
        }
    } catch (PGPException e) {
        throw e;
    } catch (Exception e) {
        throw new PGPException("Error in decryption", e);
    }
}