Gnupg RSA encryption with Bouncy Castle provider

Sucharitha.X.Panthika at Sucharitha.X.Panthika at
Thu Jun 28 14:46:43 CEST 2007

I have a problem reading the decrypted messages and the doesn't return me any bytes. See the code below that I used to encrypt data. I am writing data to file using the encryptStream returned by the following method. 

On the other hand when I write the content to the file with in the following method and later close all streams I can successfully decrypt the files. Has anyone used streaming with gpg using RSA successfully. If so, can any one share the information on how to do it.

I created a key using RSA with 1024 bytes using gpg --gen-key and used addkey to add the sub key. I am using Bouncy Castle DataGenerators, Input and outputStreams to stream the output from program and writing the encrypted content to files. However the only way I could successfully decrypt files was when I open, write and close compression, encryption and literal streams in the same method. But for my needs I would like to return a encrypted stream and let that stream be used for writing various data to files. 

  protected OutputStream encryptStream (String pPlainFilePath, boolean pAppend) throws IOException
    //PGP Encryption Data Generator
    PGPEncryptedDataGenerator encryptedDataGen = null;

    //PGP Literal Data Generator
    PGPLiteralDataGenerator literalDataGen = null;

    //PGP Compressed Data Generator
    PGPCompressedDataGenerator compressedDataGen = null;

    //The outputstream associates with different data generator for encryption, compression and writing literal data.
    OutputStream encryptStream = null;
    OutputStream compressStream = null;
    OutputStream literalStream = null;

    //EncryptionDataGenerator opens this stream to create the encrypt stream.
    OutputStream outputStream = null;

      //PGPEncryption using SymmetricKey Algorithm CAST5 (128 bit key, as per RFC 2144), Configred Message IntegrityCheck, Random Number Genarator Algorithm SHA1PRNG with Bouncy Castle provider
      encryptedDataGen = new PGPEncryptedDataGenerator(getEncryptionAlgorithm(), isMessageIntegrityCheck(), SecureRandom.getInstance(getRNGAlgorithm()), PROVIDER_NAME);

      //Add a public key encrypted session key to the encrypted object

      String pEncFilePath = null;

      if ( isAsciiOutput() )
        pEncFilePath = pPlainFilePath + ".asc";
        pEncFilePath = pPlainFilePath + ".bpg";
      //Create a FileOutputStream for the file
      outputStream = new FileOutputStream(pEncFilePath, pAppend);

      //Use ArmoredOutputStream with base64 encoding if it is Ascii
      if ( isAsciiOutput() )
        outputStream = new ArmoredOutputStream(outputStream);
      //Use EncryptiedDataGenerator to open an output stream to write the encrypted byes.
      encryptStream =, new byte[ BYTE_ARRAY_SIZE ] );

      compressedDataGen = new PGPCompressedDataGenerator(PGPCompressedData.BZIP2);

      compressStream =, new byte[ BYTE_ARRAY_SIZE ]);

      literalDataGen = new PGPLiteralDataGenerator(true);

      literalStream =, PGPLiteralData.BINARY, pEncFilePath, DateTools.getInstance().getSystemDate(), new byte[BYTE_ARRAY_SIZE]);

    catch (PGPException e)
      sLOGGER.error(e.getMessage(), e);
      if (e.getUnderlyingException() != null)
    catch (NoSuchProviderException e)
      sLOGGER.error("No Such Provider named BC [BouncyCastle]" + e.getMessage(), e);
    catch (NoSuchAlgorithmException e)
      sLOGGER.error("No Such Algorithm [" + getRNGAlgorithm() + "]" + e.getMessage(), e);
      literalStream.close(); */
    return literalStream;
    //Encrypted and compressed outputstream

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law.  If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED.  Although this transmission and
any attachments are believed to be free of any virus or other
defect that might affect any computer system into which it is
received and opened, it is the responsibility of the recipient to
ensure that it is virus free and no responsibility is accepted by
JPMorgan Chase & Co., its subsidiaries and affiliates, as
applicable, for any loss or damage arising in any way from its use.
 If you received this transmission in error, please immediately
contact the sender and destroy the material in its entirety,
whether in electronic or hard copy format. Thank you.

More information about the Gnupg-users mailing list