Example usage for org.bouncycastle.openpgp PGPLiteralDataGenerator TEXT

List of usage examples for org.bouncycastle.openpgp PGPLiteralDataGenerator TEXT

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPLiteralDataGenerator TEXT.

Prototype

char TEXT

To view the source code for org.bouncycastle.openpgp PGPLiteralDataGenerator TEXT.

Click Source Link

Document

Format tag for textual literal data

Usage

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

License:Open Source License

/**
 * Encryptes the given input and writes into the output. If the key doesn't exist nothing will happen.
 * @param input input content/*from   www  .ja v a  2s  .  c o m*/
 * @param output output as {@link ArmoredOutputStream} (BASE64)
 * @param keyID the keyID of the PublicKey that should be used
 * @throws PGPException
 * @throws IOException
 * @throws NoSuchProviderException
 */
public void encrypt(InputStream input, OutputStream output, long keyID) throws PGPException, IOException {
    PGPPublicKey encryptkey = null;

    /* take the own key if that is the ID... */
    if (keyID == publickey.getKeyID()) {
        encryptkey = publickey;
    } else {
        encryptkey = keyring.getPublicKey(keyID);
    }

    if (encryptkey != null) {

        PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator(
                PGPEncryptedDataGenerator.TRIPLE_DES, new SecureRandom(), "BC");

        try {
            encryptor.addMethod(encryptkey);
        } catch (NoSuchProviderException e) {
            LOGGER.log(Level.SEVERE,
                    "provider hasn't been loaded successfully! There is a problem with the Bouncycastle system!",
                    e);
        }
        ArmoredOutputStream armoredOut = new ArmoredOutputStream(output);
        OutputStream encryptedOut = encryptor.open(armoredOut, new byte[256]);
        PGPCompressedDataGenerator compressor = new PGPCompressedDataGenerator(PGPCompressedDataGenerator.ZIP);
        OutputStream compressedOut = compressor.open(encryptedOut);

        PGPLiteralDataGenerator literalor = new PGPLiteralDataGenerator();
        OutputStream literalOut = literalor.open(compressedOut, PGPLiteralDataGenerator.TEXT, "internal",
                new Date(), new byte[256]);

        /* copy from input to output */
        IOUtils.copy(input, literalOut);

        literalor.close();
        compressor.close();
        encryptor.close();
        armoredOut.close();
    }
}