title: PGP Compress and Encrypt
body: <p>I’m using Bouncy Castles to compress and encrypt some data.  The compress method fails with a null reference exception. The following method performs compression:</p>
  
  <pre><code>private byte[] Compress(byte[] data)
          {
              using (MemoryStream outStream = new MemoryStream())
              {
                  PgpCompressedDataGenerator pgpCompressedDataGenerator = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
                  using (Stream compressedStream = pgpCompressedDataGenerator.Open(outStream))
                  {
                      PgpLiteralDataGenerator pgpLiteralDataGenerator = new PgpLiteralDataGenerator();
  
                      using (Stream literalDataStream = pgpLiteralDataGenerator.Open(compressedStream, PgpLiteralData.Binary, null, data.Length, DateTime.UtcNow))
                      {
                          literalDataStream.Write(data, 0, data.Length);
  
                          literalDataStream.Close();
  
                          pgpCompressedDataGenerator.Close();
  
                          return outStream.ToArray();
                      }
                  }
              }
          }
  </code></pre>
  
  <p>The following line causes the bulk reference exception:</p>
  
  <pre><code>pgpLiteralDataGenerator.Open(compressedStream, PgpLiteralData.Binary, null, data.Length, DateTime.UtcNow))
  </code></pre>
  
  <p>However I don’t know why - if I leave compression out, the encryption part works as expected.  Do I need another library to perform the compression,</p>
  
  <p>Mark</p>