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:
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();
}
}
}
}
The following line causes the bulk reference exception:
pgpLiteralDataGenerator.Open(compressedStream, PgpLiteralData.Binary, null, data.Length, DateTime.UtcNow))
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,
Mark
It turns out the null parameter in the call below was causing the problem:
pgpLiteralDataGenerator.Open(compressedStream, PgpLiteralData.Binary, null, data.Length, DateTime.UtcNow))
This code was a direct copy from another project, however in that project the Bouncy Castles API was brought in with iTextSharp - I'm assuming there's a slight difference in implementation.