Runtime: Cannot decompress Rijndael encrypted Gzip

Created on 16 Oct 2017  路  11Comments  路  Source: dotnet/runtime

Hello,

we are using Rijndael encrypted gzips. The following code works just fine in .NET Framework 4.6.1 but throws exception in .NET Core 2.0

using (var rijAlg = Rijndael.Create())
{
    rijAlg.Key = _configK;
    rijAlg.IV = _configI;
    rijAlg.BlockSize = 128;
    rijAlg.Padding = PaddingMode.PKCS7;

    // Create a decrytor to perform the stream transform.
    var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

    // Create the streams used for decryption. 
    using (var sourceStream = new MemoryStream(buffer))
    {
        using (var decryptStream = new CryptoStream(sourceStream, decryptor, CryptoStreamMode.Read))
        {
            if (isGzipped)
            {
                using (var unzipStream = new System.IO.Compression.GZipStream(decryptStream, System.IO.Compression.CompressionMode.Decompress)) 
                {
                    using (var targetStream = new MemoryStream())
                    {
                        unzipStream.CopyTo(targetStream);
                        Content = targetStream.ToArray();
                    }
                }
            }
            else
            {
                using (var targetStream = new MemoryStream())
        {
            decryptStream.CopyTo(targetStream);
            Content = targetStream.ToArray();
        }
        }
    }
    }
}

The exception is:
The archive entry was compressed using an unsupported compression method.

Stack trace:

   at System.IO.Compression.Inflater.Inflate(FlushCode flushCode)
   at System.IO.Compression.Inflater.ReadInflateOutput(Byte* bufPtr, Int32 length, FlushCode flushCode, Int32& bytesRead)
   at System.IO.Compression.Inflater.InflateVerified(Byte* bufPtr, Int32 length)
   at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
   at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Compression.GZipStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)

Maybe the decryptor has a bug which results in an invalid decrypted data and that's why GzipStream fails to decompress it?

area-System.Security bug

Most helpful comment

All 11 comments

@ianhays can indicate whether tihs is expected.

I've created a test console application in which I Gzipped a string, encrypted it with Rijndael encryption, then tried to decrypt and decompress it. The result is that if I don't set the SymmetricAlgorithm.Blocksize property to 128 bits, then everything works fine.
As soon as I set it to 128 bits I get the System.IO.InvalidDataException. The message is the same as mentioned in my original comment:
The archive entry was compressed using an unsupported compression method.

This error is not present in .NET Framework 4.6.2 (tested it with this very same test class).

I attached my test console project.
Cryptography.zip

The result is that if I don't set the SymmetricAlgorithm.Blocksize property to 128 bits, then everything works fine.

Interesting... the default BlockSize is 128, so specifying it explicitly shouldn't make a difference.

edit: By the way, thanks for the highly detailed repro program @gabor182. Makes a huge difference in debugging.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aggieben picture aggieben  路  3Comments

v0l picture v0l  路  3Comments

jamesqo picture jamesqo  路  3Comments

jzabroski picture jzabroski  路  3Comments

matty-hall picture matty-hall  路  3Comments