Example usage for org.bouncycastle.openpgp PGPOnePassSignature update

List of usage examples for org.bouncycastle.openpgp PGPOnePassSignature update

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPOnePassSignature update.

Prototype

public void update(byte[] bytes) 

Source Link

Usage

From source file:alpha.offsync.security.OpenPGPSecurityUtility.java

License:Apache License

@Override
public void verify(OutputStream outputStream, final InputStream inputStream) {

    try {/*  www  .  ja  v  a2  s  .com*/
        final File keyFile = this.publicKeyRing;

        final InputStream in = PGPUtil.getDecoderStream(inputStream);

        PGPObjectFactory pgpFact = new PGPObjectFactory(in);

        final PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();

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

        final PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact.nextObject();

        final PGPOnePassSignature ops = p1.get(0);

        final PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();

        final InputStream dIn = p2.getInputStream();
        int ch;
        final PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(
                PGPUtil.getDecoderStream(new FileInputStream(keyFile)));

        final PGPPublicKey key = pgpRing.getPublicKey(ops.getKeyID());

        ops.init(new BcPGPContentVerifierBuilderProvider(), key);

        while ((ch = dIn.read()) >= 0) {
            ops.update((byte) ch);
            outputStream.write(ch);
        }

        outputStream.close();

        final PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();

        if (!ops.verify(p3.get(0))) {
            outputStream = null;
        }
    } catch (final FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final SignatureException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final PGPException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

License:Open Source License

/**
 * Check a one-pass signature/* w  w  w. j a v a2 s. c  om*/
 */
private boolean checkOnePassSignature(OutputStream out, PGPOnePassSignatureList p1, PGPObjectFactory pgpFact,
        PGPPublicKeyRingCollection pgpRing) throws PGPException {
    try {
        PGPOnePassSignature ops = null;
        PGPPublicKey key = null;

        int count = 0;
        while (count < p1.size()) {
            ops = p1.get(count);
            key = pgpRing.getPublicKey(ops.getKeyID());
            if (key != null) {
                break;
            }

            count++;
        }

        if (key == null) {
            throw new PGPException("Corresponding public key not found");
        }

        if (key.isRevoked()) {
            String keyId = Long.toHexString(key.getKeyID()).substring(8);
            System.out.println("Warning: Signing key (0x" + keyId + ") has been revoked");
            // throw new PGPException("Signing key (0x"+keyId+") has been revoked");
        }

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

        //            if (outputFilename == null) {
        //                outputFilename = ld.getFileName();
        //            }
        //
        //            FileOutputStream out = new FileOutputStream(outputFilename);

        InputStream dataIn = ld.getInputStream();

        ops.initVerify(key, "BC");

        int ch;
        while ((ch = dataIn.read()) >= 0) {
            ops.update((byte) ch);
            out.write(ch);
        }

        out.close();

        PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();

        return ops.verify(p3.get(0));
    } catch (PGPException e) {
        throw e;
    } catch (Exception e) {
        throw new PGPException("Error in verification", e);
    }
}

From source file:com.goodvikings.cryptim.api.KeyRing.java

License:BEER-WARE LICENSE

public boolean decryptVerifyMessage(InputStream in, OutputStream out, String jid)
        throws IOException, PGPException, SignatureException {
    in = new ArmoredInputStream(in);

    PGPObjectFactory plainFact = new PGPObjectFactory(
            ((PGPPublicKeyEncryptedData) ((PGPEncryptedDataList) new PGPObjectFactory(in).nextObject())
                    .getEncryptedDataObjects().next())
                            .getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider(PROVIDER)
                                    .build(kp.getPrivateKey())));

    PGPOnePassSignatureList onePassSignatureList = null;
    PGPSignatureList signatureList = null;
    PGPCompressedData compressedData = null;

    Object obj = plainFact.nextObject();
    ByteArrayOutputStream actualOutput = new ByteArrayOutputStream();

    while (obj != null) {
        if (obj instanceof PGPCompressedData) {
            compressedData = (PGPCompressedData) obj;
            plainFact = new PGPObjectFactory(compressedData.getDataStream());
            obj = plainFact.nextObject();
        }//from  ww  w.j  av  a2 s .c  o m
        if (obj instanceof PGPLiteralData) {
            Streams.pipeAll(((PGPLiteralData) obj).getInputStream(), actualOutput);
        } else if (obj instanceof PGPOnePassSignatureList) {
            onePassSignatureList = (PGPOnePassSignatureList) obj;
        } else if (obj instanceof PGPSignatureList) {
            signatureList = (PGPSignatureList) obj;
        } else {
            throw new PGPException("message unknown message type.");
        }
        obj = plainFact.nextObject();
    }

    actualOutput.close();
    byte[] output = actualOutput.toByteArray();

    PGPOnePassSignature ops = onePassSignatureList.get(0);
    ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider(PROVIDER), keys.get(jid));
    ops.update(output);

    out.write(output);
    out.flush();
    out.close();

    return ops.verify(signatureList.get(0));
}

From source file:com.verhas.licensor.License.java

License:Open Source License

/**
 * Open an encoded license from input stream and decode and load it. If the
 * file can not be loaded or is not signed properly then the method {@see
 * #isVerified()} will return false.//from w ww.  j  av  a 2 s  .  c om
 * <p>
 * Otherwise the license will be loaded and can be used.
 * 
 * @param in
 * @throws IOException
 * @throws PGPException
 */
public void setLicenseEncoded(InputStream in) throws IOException, PGPException {
    final ByteArrayInputStream keyIn = new ByteArrayInputStream(publicKeyRing);
    in = PGPUtil.getDecoderStream(in);

    PGPObjectFactory pgpFact = new PGPObjectFactory(in);
    final PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();
    pgpAssertNotNull(c1);
    pgpFact = new PGPObjectFactory(c1.getDataStream());
    final PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact.nextObject();

    pgpAssertNotNull(p1);
    final PGPOnePassSignature ops = p1.get(0);
    final PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();

    pgpAssertNotNull(p2);
    final InputStream dIn = p2.getInputStream();
    pgpAssertNotNull(dIn);
    int ch;
    final PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
    pgpAssertNotNull(ops);
    decodeKeyId = ops.getKeyID();
    if (decodeKeyId == null) {
        // there is no key in the key ring that can decode the license
        verified = false;
        licenseProperties = null;
    } else {
        final PGPPublicKey decodeKey = pgpRing.getPublicKey(decodeKeyId);
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ops.initVerify(decodeKey, "BC");
            while ((ch = dIn.read()) >= 0) {
                ops.update((byte) ch);
                out.write(ch);
            }
            final PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();

            if (ops.verify(p3.get(0))) {
                setLicense(new String(out.toByteArray()));
                verified = true;
            } else {
                verified = false;
                licenseProperties = null;
            }
        } catch (final Exception e) {
            verified = false;
            licenseProperties = null;
        }
    }
}

From source file:crypttools.PGPCryptoBC.java

License:Open Source License

public boolean validateData(String data, String publicKey) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    File fileToVerify = File.createTempFile("temp", ".privateScrap");
    FileUtils.writeStringToFile(fileToVerify, data);

    File publicKeyFile = File.createTempFile("temp", ".publicScrap");
    // Creates an exception
    //        System.out.println(this.armoredPublicKey);
    //        String armoredKeyString = getPublicKey();
    //        System.out.println(armoredKeyString);
    FileUtils.writeStringToFile(publicKeyFile, publicKey);
    //FileUtils.writeStringToFile(publicKeyFile, new String(this.armoredPublicKey, "UTF-8"));

    try {/*  w w  w.jav a  2  s .  co m*/
        InputStream in = PGPUtil.getDecoderStream(new FileInputStream(fileToVerify));

        PGPObjectFactory pgpObjFactory = new PGPObjectFactory(in);
        PGPCompressedData compressedData = (PGPCompressedData) pgpObjFactory.nextObject();

        //Get the signature from the file

        pgpObjFactory = new PGPObjectFactory(compressedData.getDataStream());
        PGPOnePassSignatureList onePassSignatureList = (PGPOnePassSignatureList) pgpObjFactory.nextObject();
        PGPOnePassSignature onePassSignature = onePassSignatureList.get(0);

        //Get the literal data from the file

        PGPLiteralData pgpLiteralData = (PGPLiteralData) pgpObjFactory.nextObject();
        InputStream literalDataStream = pgpLiteralData.getInputStream();

        InputStream keyIn = new FileInputStream(publicKeyFile);
        PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
        PGPPublicKey key = pgpRing.getPublicKey(onePassSignature.getKeyID());

        FileOutputStream literalDataOutputStream = new FileOutputStream(pgpLiteralData.getFileName());
        onePassSignature.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key);

        int ch;
        while ((ch = literalDataStream.read()) >= 0) {
            onePassSignature.update((byte) ch);
            literalDataOutputStream.write(ch);
        }

        literalDataOutputStream.close();

        //Get the signature from the written out file

        PGPSignatureList p3 = (PGPSignatureList) pgpObjFactory.nextObject();
        PGPSignature signature = p3.get(0);

        //Verify the two signatures
        boolean valid = onePassSignature.verify(signature);
        return valid;
    } catch (Exception e) {
        System.out.println("Got an Exception: " + e.getMessage());
        return false;
        //do something clever with the exception
    } finally {
        fileToVerify.delete();
        publicKeyFile.delete();
    }
}

From source file:eu.mrbussy.security.crypto.pgp.PGPDecryptor.java

License:Open Source License

public InputStream decryptFile(InputStream in) throws Exception {
    InputStream is = null;//from  w ww  .j  a  v a2s  .c o  m
    byte[] bytes = null;
    InputStream keyIn = new FileInputStream(new File(privateKeyFilePath));
    char[] passwd = password.toCharArray();
    in = PGPUtil.getDecoderStream(in);

    PGPObjectFactory pgpF = new PGPObjectFactory(in);
    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();
    }

    //
    // find the secret key
    //
    Iterator<PGPPublicKeyEncryptedData> it = enc.getEncryptedDataObjects();
    PGPPrivateKey sKey = null;
    PGPPublicKeyEncryptedData pbe = null;
    while (sKey == null && it.hasNext()) {
        pbe = it.next();
        sKey = PGPUtils.findPrivateKey(keyIn, pbe.getKeyID(), passwd);
    }

    if (sKey == null) {
        throw new IllegalArgumentException("secret key for message not found.");
    }

    InputStream clear = pbe.getDataStream(sKey, "BC");
    PGPObjectFactory plainFact = new PGPObjectFactory(clear);
    Object message = plainFact.nextObject();
    PGPObjectFactory pgpFact = null;
    if (message instanceof PGPCompressedData) {
        PGPCompressedData cData = (PGPCompressedData) message;
        pgpFact = new PGPObjectFactory(cData.getDataStream());
        message = pgpFact.nextObject();
    }

    PGPOnePassSignature ops = null;
    if (message instanceof PGPOnePassSignatureList) {
        if (isSigned) {
            PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) message;
            ops = p1.get(0);
            long keyId = ops.getKeyID();
            PGPPublicKey signerPublicKey = PGPUtils.readPublicKey(signingPublicKeyFilePath, keyId);
            ops.initVerify(signerPublicKey, "BC");
        }
        message = pgpFact.nextObject();
    }

    if (message instanceof PGPLiteralData) {
        PGPLiteralData ld = (PGPLiteralData) message;
        if (pbe.isIntegrityProtected()) {
            if (!pbe.verify()) {
                throw new PGPException("message failed integrity check");
            }
        }
        is = ld.getInputStream();
        bytes = IOUtils.toByteArray(is);

        if (isSigned) {
            ops.update(bytes);
            PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();
            if (!ops.verify(p3.get(0))) {
                throw new PGPException("Signature verification failed!");
            }
        }
    } else {
        throw new PGPException("message is not a simple encrypted file - type unknown.");
    }
    return new ByteArrayInputStream(bytes);
}

From source file:google.registry.rde.BouncyCastleTest.java

License:Open Source License

@Test
public void testSignVerify_OnePass() throws Exception {
    // Load the keys.
    PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
    PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
    PGPPublicKey publicKey = publicKeyRing.getPublicKey();
    PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());

    // Sign the data and write signature data to "signatureFile".
    PGPSignatureGenerator signer = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
    signer.init(PGPSignature.BINARY_DOCUMENT, privateKey);
    addUserInfoToSignature(publicKey, signer);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    signer.generateOnePassVersion(false).encode(output);
    signer.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
    signer.generate().encode(output);/* ww  w  . j a  va 2s.  c o m*/
    byte[] signatureFileData = output.toByteArray();
    logger.info(".sig file data: " + dumpHex(signatureFileData));

    // Load algorithm information and signature data from "signatureFileData".
    PGPSignature sig;
    PGPOnePassSignature onePass;
    try (ByteArrayInputStream input = new ByteArrayInputStream(signatureFileData)) {
        PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
        PGPOnePassSignatureList onePassList = (PGPOnePassSignatureList) pgpFact.nextObject();
        PGPSignatureList sigList = (PGPSignatureList) pgpFact.nextObject();
        assertThat(onePassList.size()).isEqualTo(1);
        assertThat(sigList.size()).isEqualTo(1);
        onePass = onePassList.get(0);
        sig = sigList.get(0);
    }

    // Use "onePass" and "sig" to verify "publicKey" signed the text.
    onePass.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
    onePass.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
    assertThat(onePass.verify(sig)).isTrue();

    // Verify that they DIDN'T sign the text "hello monster".
    onePass.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
    onePass.update("hello monster".getBytes(UTF_8));
    assertThat(onePass.verify(sig)).isFalse();
}

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

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

    try {/* w  w w.  j a  v a 2s  . co  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:org.kontalk.crypto.Coder.java

License:Open Source License

private static DecryptionResult decryptAndVerify(InputStream encryptedStream, PersonalKey myKey,
        PGPPublicKey senderKey) {//from   w w  w .  j  a  va2s  .c om
    // note: the signature is inside the encrypted data

    DecryptionResult result = new DecryptionResult();

    PGPObjectFactory pgpFactory = new PGPObjectFactory(encryptedStream);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try { // catch all IO and PGP exceptions

        // the first object might be a PGP marker packet
        Object o = pgpFactory.nextObject(); // nullable
        if (!(o instanceof PGPEncryptedDataList)) {
            o = pgpFactory.nextObject(); // nullable
        }

        if (!(o instanceof PGPEncryptedDataList)) {
            LOGGER.warning("can't find encrypted data list in data");
            result.errors.add(Error.INVALID_DATA);
            return result;
        }
        PGPEncryptedDataList encDataList = (PGPEncryptedDataList) o;

        // check if secret key matches our encryption keyID
        Iterator<?> it = encDataList.getEncryptedDataObjects();
        PGPPrivateKey sKey = null;
        PGPPublicKeyEncryptedData pbe = null;
        long myKeyID = myKey.getPrivateEncryptionKey().getKeyID();
        while (sKey == null && it.hasNext()) {
            Object i = it.next();
            if (!(i instanceof PGPPublicKeyEncryptedData))
                continue;
            pbe = (PGPPublicKeyEncryptedData) i;
            if (pbe.getKeyID() == myKeyID)
                sKey = myKey.getPrivateEncryptionKey();
        }
        if (sKey == null || pbe == null) {
            LOGGER.warning("private key for message not found");
            result.errors.add(Error.INVALID_PRIVATE_KEY);
            return result;
        }

        InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(sKey));

        PGPObjectFactory plainFactory = new PGPObjectFactory(clear);

        Object object = plainFactory.nextObject(); // nullable

        if (!(object instanceof PGPCompressedData)) {
            LOGGER.warning("data packet not compressed");
            result.errors.add(Error.INVALID_DATA);
            return result;
        }

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

        object = pgpFact.nextObject(); // nullable

        // the first object could be the signature list
        // get signature from it
        PGPOnePassSignature ops = null;
        if (object instanceof PGPOnePassSignatureList) {
            PGPOnePassSignatureList signatureList = (PGPOnePassSignatureList) object;
            // there is a signature list, so we assume the message is signed
            // (makes sense)
            result.signing = Signing.SIGNED;

            if (signatureList.isEmpty()) {
                LOGGER.warning("signature list is empty");
                result.errors.add(Error.INVALID_SIGNATURE_DATA);
            } else {
                ops = signatureList.get(0);
                ops.init(new BcPGPContentVerifierBuilderProvider(), senderKey);
            }
            object = pgpFact.nextObject(); // nullable
        } else {
            LOGGER.warning("signature list not found");
            result.signing = Signing.NOT;
        }

        if (!(object instanceof PGPLiteralData)) {
            LOGGER.warning("unknown packet type: " + object.getClass().getName());
            result.errors.add(Error.INVALID_DATA);
            return result;
        }

        PGPLiteralData ld = (PGPLiteralData) object;
        InputStream unc = ld.getInputStream();
        int ch;
        while ((ch = unc.read()) >= 0) {
            outputStream.write(ch);
            if (ops != null)
                try {
                    ops.update((byte) ch);
                } catch (SignatureException ex) {
                    LOGGER.log(Level.WARNING, "can't read signature", ex);
                }
        }

        result.decryptedStream = Optional.of(outputStream);

        if (ops != null) {
            result = verifySignature(result, pgpFact, ops);
        }

        // verify message integrity
        if (pbe.isIntegrityProtected()) {
            if (!pbe.verify()) {
                LOGGER.warning("message integrity check failed");
                result.errors.add(Error.INVALID_INTEGRITY);
            }
        } else {
            LOGGER.warning("message is not integrity protected");
            result.errors.add(Error.NO_INTEGRITY);
        }

    } catch (IOException | PGPException ex) {
        LOGGER.log(Level.WARNING, "can't decrypt message", ex);
        result.errors.add(Error.UNKNOWN_ERROR);
    }

    return result;
}

From source file:org.opentestsystem.delivery.testreg.transformer.GpgVerifier.java

License:Open Source License

public byte[] decryptAndVerify(File encryptedSignedFile) throws IOException, SignatureException, PGPException {

    byte[] output = null;

    InputStream in = PGPUtil.getDecoderStream(new FileInputStream(encryptedSignedFile));
    InputStream publicKeyIn = encryptor.getStreamForPath(publicKeyringLocation);

    ByteArrayOutputStream fOut = new ByteArrayOutputStream();

    PGPObjectFactory pgpF = new PGPObjectFactory(in);
    PGPEncryptedDataList enc;//  ww  w  .java 2s .  c o  m

    Object o = pgpF.nextObject();
    //
    // the first object might be a PGP marker packet.
    //

    while (!(o instanceof PGPEncryptedDataList)) {
        o = pgpF.nextObject();
    }

    if (o instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) o;
    } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
    }

    //
    // find the secret key
    //
    Iterator<?> it = enc.getEncryptedDataObjects();
    PGPPrivateKey sKey = null;
    PGPPublicKeyEncryptedData pbe = null;

    while (sKey == null && it.hasNext()) {
        pbe = (PGPPublicKeyEncryptedData) it.next();
        InputStream secretKeyringInputStream = encryptor.getStreamForPath(secretKeyringLocation);

        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
                PGPUtil.getDecoderStream(secretKeyringInputStream));
        PGPSecretKey pgpSecKey = pgpSec.getSecretKey(pbe.getKeyID());
        if (pgpSecKey == null) {
            fail("could not find secret key");
        }

        PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(
                new BcPGPDigestCalculatorProvider()).build(LANDINGZONE_PASS);

        sKey = pgpSecKey.extractPrivateKey(decryptor);
    }

    if (sKey == null) {
        throw new IllegalArgumentException("secret key for message not found.");
    }

    InputStream clear = pbe
            .getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(sKey));

    PGPObjectFactory plainFact = new PGPObjectFactory(clear);

    Object message = null;

    PGPOnePassSignatureList onePassSignatureList = null;
    PGPSignatureList signatureList = null;
    PGPCompressedData compressedData = null;

    message = plainFact.nextObject();
    ByteArrayOutputStream actualOutput = new ByteArrayOutputStream();

    while (message != null) {
        LOGGER.debug("decrypted message: " + message.toString());
        if (message instanceof PGPCompressedData) {
            compressedData = (PGPCompressedData) message;
            plainFact = new PGPObjectFactory(compressedData.getDataStream());
            message = plainFact.nextObject();
        }

        if (message instanceof PGPLiteralData) {
            // have to read it and keep it somewhere.
            Streams.pipeAll(((PGPLiteralData) message).getInputStream(), actualOutput);
        } else if (message instanceof PGPOnePassSignatureList) {
            onePassSignatureList = (PGPOnePassSignatureList) message;
        } else if (message instanceof PGPSignatureList) {
            signatureList = (PGPSignatureList) message;
        } else {
            throw new PGPException("message unknown message type.");
        }
        message = plainFact.nextObject();
    }
    actualOutput.close();
    PGPPublicKey publicKey = null;
    output = actualOutput.toByteArray();

    if (onePassSignatureList == null || signatureList == null) {
        throw new PGPException("Signatures not found.");
    } else {

        for (int i = 0; i < onePassSignatureList.size(); i++) {
            PGPOnePassSignature ops = onePassSignatureList.get(0);
            LOGGER.debug("verifier : " + ops.getKeyID());
            PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(
                    PGPUtil.getDecoderStream(publicKeyIn));
            publicKey = pgpRing.getPublicKey(ops.getKeyID());
            if (publicKey != null) {
                ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), publicKey);
                ops.update(output);
                PGPSignature signature = signatureList.get(i);
                // apparently the signature can only be verified once?? if the verify method is called a 2nd time it
                // will fail
                boolean signatureVerified = ops.verify(signature);
                assertThat(signatureVerified, is(true));
                if (signatureVerified) {
                    Iterator<?> userIds = publicKey.getUserIDs();
                    while (userIds.hasNext()) {
                        String userId = (String) userIds.next();
                        LOGGER.debug("Signed by " + userId);
                    }
                    LOGGER.debug("Signature verified");
                } else {
                    throw new SignatureException("Signature verification failed");
                }
            }
        }

    }

    if (pbe.isIntegrityProtected() && !pbe.verify()) {
        throw new PGPException("Data is integrity protected but integrity is lost.");
    } else if (publicKey == null) {
        throw new SignatureException("Signature not found");
    } else {
        fOut.write(output);
        fOut.flush();
        fOut.close();

        LOGGER.debug("decrypt and verify output: " + fOut.toString());
    }

    return output;
}