Example usage for org.bouncycastle.openpgp PGPLiteralData getDataStream

List of usage examples for org.bouncycastle.openpgp PGPLiteralData getDataStream

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPLiteralData getDataStream.

Prototype

public InputStream getDataStream() 

Source Link

Document

Return the input stream representing the data stream.

Usage

From source file:de.jtheuer.diki.lib.pgp.PGPHandler.java

License:Open Source License

/**
 * @param input a BASE64 encoded {@link InputStream}
 * @param output/*w w w .ja v  a 2s  . co m*/
 * @throws IOException
 * @throws PGPException 
 */
public void decrypt(InputStream input, OutputStream output) throws IOException, PGPException {
    PGPObjectFactory in = new PGPObjectFactory(new ArmoredInputStream(input));
    Object object = in.nextObject();
    if (object instanceof PGPEncryptedDataList) {
        PGPEncryptedDataList pgpstream = (PGPEncryptedDataList) object;
        Iterator<?> it = pgpstream.getEncryptedDataObjects();

        /* iterate over content until a message has been found */
        while (it.hasNext()) {
            Object o = it.next();
            if (o instanceof PGPPublicKeyEncryptedData) {
                PGPPublicKeyEncryptedData pgp = (PGPPublicKeyEncryptedData) o;
                InputStream decrypted = pgp.getDataStream(privatekey, new BouncyCastleProvider());

                /* the stream is still zipped, so we have to unzip it... */
                PGPObjectFactory unzip_object = new PGPObjectFactory(decrypted);
                PGPCompressedData unzipped = (PGPCompressedData) unzip_object.nextObject();

                /* and literal ... */
                PGPObjectFactory literal_object = new PGPObjectFactory(unzipped.getDataStream());
                PGPLiteralData literal = (PGPLiteralData) literal_object.nextObject();

                IOUtils.copy(literal.getDataStream(), output);

                break;
            }
        }
    } else {
        throw new PGPException(
                "Stream is not a PGPEncryptedDataList stream :" + object.getClass().getSimpleName());
    }
}

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

License:Open Source License

/**
 * Opens a new {@link Input} for reading the original contents (Reading Step 3/3)
 *
 * <p>This is the final step in reading a ghostryde file. After calling this method, you should
 * call the read methods on the returned {@link InputStream}.
 *
 * @param input is the value returned by {@link #openDecompressor}.
 * @throws IOException//  w w w . j a v  a2  s. co  m
 * @throws PGPException
 */
@CheckReturnValue
public Input openInput(@WillNotClose Decompressor input) throws IOException, PGPException {
    PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input"));
    PGPLiteralData literal = pgpCast(fact.nextObject(), PGPLiteralData.class);
    DateTime modified = new DateTime(literal.getModificationTime(), UTC);
    return new Input(literal.getDataStream(), literal.getFileName(), modified);
}

From source file:uk.co.platosys.dinigma.CryptoEngine.java

License:GNU General Public License

/**
 *  Decrypts an InputStream to a Document
 *
 * @param inputStream// ww w.ja  va2s.  c o m
 * @param key
 * @param passphrase
 * @return
 * @throws Exception
 */

public static String decrypt(InputStream inputStream, Key key, char[] passphrase)
        throws MinigmaException, DecryptionException, java.io.IOException {
    InputStream in;
    PGPObjectFactory pgpObjectFactory;
    PGPEncryptedDataList pgpEncryptedDataList = null;
    PGPPrivateKey privateKey = null;
    PGPPublicKeyEncryptedData pgpPublicKeyEncryptedData = null;
    Object compressedObject = null;
    PGPLiteralData literalData = null;
    //First get a  PGPEncryptedDataList from the input stream.
    try {
        in = PGPUtil.getDecoderStream(inputStream);
        pgpObjectFactory = new PGPObjectFactory(in, new JcaKeyFingerprintCalculator());
        Object object = pgpObjectFactory.nextObject();
        if (object instanceof PGPEncryptedDataList) {
            //the EncryptedDataList is either the first object;
            pgpEncryptedDataList = (PGPEncryptedDataList) object;
        } else {
            //or the next
            pgpEncryptedDataList = (PGPEncryptedDataList) pgpObjectFactory.nextObject();
        }

        if (pgpEncryptedDataList == null) {
            throw new MinigmaException("couldn't find encrypted data list");
        }
    } catch (Exception e) {
        //Log.d(TAG,"Minigma-unLock() 1: error reading encrypted data list", e);
        throw new MinigmaException("error reading encrypted data list", e);
    }
    // now get encrypted objects from the list.
    try {
        //Log.d(TAG, "Minigma-unLock() 2 start");
        @SuppressWarnings("unchecked")
        Iterator<PGPPublicKeyEncryptedData> it = pgpEncryptedDataList.getEncryptedDataObjects();
        //Log.d(TAG, "Minigma-unLock() 2: EncryptedDataList size = "+Integer.toString(pgpEncryptedDataList.size())+", now got its iterator");
        JcePBESecretKeyDecryptorBuilder keyDecryptorBuilder = new JcePBESecretKeyDecryptorBuilder();
        keyDecryptorBuilder.setProvider(BouncyCastleProvider.PROVIDER_NAME);
        while (it.hasNext() && privateKey == null) {
            pgpPublicKeyEncryptedData = it.next();
            long keyID = pgpPublicKeyEncryptedData.getKeyID();
            //Log.d(TAG, "Minigma-unLock() 2: data was encrypted with key:"+ Long.toHexString(keyID));
            PGPSecretKey secretKey = key.getDecryptionKey(keyID);
            if (secretKey == null) {
                //Log.d(TAG, "Minigma-unLock() 2: bad key, no decryption key");
                throw new DecryptionException("2: bad key, no decryption key");
            }
            if (secretKey.getKeyID() == keyID) {
                privateKey = key.getDecryptionKey(keyID)
                        .extractPrivateKey(keyDecryptorBuilder.build(passphrase));
                //Log.d(TAG,"Minigma-unLock() 2: got private key");
            } else {
                //Log.d(TAG, "Engima-unLock() 2: not this time, round again.");
            }
        }
        if (privateKey == null) {

            throw new DecryptionException("Minigma-unLock() 2: decryption key doesn't fit any of the locks");
        }
    } catch (Exception e) {

        throw new MinigmaException("A problem arose during decryption", e);
    }

    try {

        PublicKeyDataDecryptorFactory dataDecryptorFactory = new BcPublicKeyDataDecryptorFactory(privateKey);
        InputStream decryptedStream = pgpPublicKeyEncryptedData.getDataStream(dataDecryptorFactory);
        JcaPGPObjectFactory compressedFactory = new JcaPGPObjectFactory(decryptedStream);
        compressedObject = compressedFactory.nextObject();

    } catch (Exception e) {

        throw new MinigmaException("Minigma-unLock() 3: error reading encrypted data stream", e);
    }
    try {

        PGPCompressedData clearCompressedData = (PGPCompressedData) compressedObject;
        Object uncompressedObject = null;
        JcaPGPObjectFactory uncompressedFactory = null;

        InputStream inputStream2 = clearCompressedData.getDataStream();

        uncompressedFactory = new JcaPGPObjectFactory(inputStream2);

        uncompressedObject = uncompressedFactory.nextObject();

        if (uncompressedObject instanceof PGPOnePassSignatureList) {
            // and the next object should be literal data:
            uncompressedObject = uncompressedFactory.nextObject();
            if (uncompressedObject instanceof PGPLiteralData) {
                literalData = (PGPLiteralData) uncompressedObject;
            } else {
                //unrecognised object;
                throw new MinigmaException("Minigma-unLock() 4: unrecognised object: A "
                        + uncompressedObject.getClass().getName());

            }
            uncompressedObject = uncompressedFactory.nextObject();
            if (uncompressedObject instanceof PGPSignatureList) {
            } else {
                //unrecognised object;
                throw new MinigmaException(
                        "Minigma-unlock() 4: unrecognised object B " + uncompressedObject.getClass().getName());
            }
        } else if (uncompressedObject instanceof PGPLiteralData) {
            literalData = (PGPLiteralData) uncompressedObject;
        } else {
            //unrecognised object
            throw new MinigmaException(
                    "Minigma-unLock() 4: unrecognised object C " + uncompressedObject.getClass().getName());

        }
    } catch (Exception e) {
        throw new MinigmaException("Minigma-unLock() 4: error getting decompressed object", e);

    }

    InputStream inputStream1 = literalData.getDataStream();
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream1.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    }
    return result.toString("UTF-8");
}