[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bouncycastle-crypto-dev
Subject:    Re: [dev-crypto] Problem streaming the encryption with PGP, BC and
From:       David Hook <dgh () bund ! com ! au>
Date:       2007-06-29 0:03:39
Message-ID: 1183075419.16688.89.camel () anteater ! myretsu ! com
[Download RAW message or body]


The simplest way to do this would be to create your own stream class
which wraps the literal data stream and then calls all the close methods
for the others when it gets closed.

Regards,

David

On Thu, 2007-06-28 at 12:09 -0700, wcharitap wrote:
> I have a problem reading the decrypted messages and the decryptStream.read()
> 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 "open", "write", "close" the streams in the same
> method I can successfully decrypt the files but fail to decrypt when I use
> the outputstream elsewhere. Has anyone used streaming with gpg using RSA
> successfully. If so, can any one share the information if I am missing
> something. I have tried commenting each close method in finally block.
> 
> 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 in several modules across the
> project for writing the encrypted content to file and then close by using
> the stream.close().
> 
>   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;
> 
>     try
>     {
>       //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
>       encryptedDataGen.addMethod(getPublicKey());
> 
>       String pEncFilePath = null;
> 
>       if ( isAsciiOutput() )
>       {
>         pEncFilePath = pPlainFilePath + ".asc";
>       }
>       else
>       {
>         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 = encryptedDataGen.open(outputStream, new byte[
> BYTE_ARRAY_SIZE ] );
> 
>       compressedDataGen = new
> PGPCompressedDataGenerator(PGPCompressedData.BZIP2);
> 
>       compressStream = compressedDataGen.open(encryptStream, new byte[
> BYTE_ARRAY_SIZE ]);
> 
>       literalDataGen = new PGPLiteralDataGenerator(true);
> 
>       literalStream = literalDataGen.open(compressStream,
> PGPLiteralData.BINARY, pEncFilePath,
> DateTools.getInstance().getSystemDate(), new byte[BYTE_ARRAY_SIZE]);
>      /*  if I write here using this stream I can encrypt and decypt
> successfully.
>       listeralStream.write("TEstData write inline is successful but if you
> write out side this method decrypt fails".getBytes());
>      */
>     }
>     catch (PGPException e)
>     {
>       sLOGGER.error(e.getMessage(), e);
>       if (e.getUnderlyingException() != null)
>       {
>         sLOGGER.error(e.getUnderlyingException());
>       }
>     }
>     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);
>     }
>     finally
>     {
>       /*literalDataGen.close();
>       literalStream.close(); */
>       compressedDataGen.close();
>       compressStream.close();
>       encryptedDataGen.close();
>       encryptStream.close();
>     }
>     return literalStream;
>     //Encrypted and compressed outputstream
>   }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic