Using compression with encryption

G

Guest

Hi,

I am developing an internal compression format. This format stores multiple
files (into 1 file) and uses compression (System.IO.Compression.GZipStream)
and

encryption (System.Security.Cryptography.RijndaelManaged) as well.

If I use compression only, everything works OK. Also, If I only use
encryption, there is no problem. The problem is, when I combine encryption
with

compression.

My approach in adding file into archive is (simplified):

1. write rijndael IV into output file stream (fs)
2. open cryptographic output stream above this stream
CryptoStream cs = new CryptoStream(fs, encryptor, CryptoStreamMode.Write);
3. open gzip output stream above cs
GZipStream gs = new GZipStream(cs, CompressionMode.Compress, true);
4. write bytes from input file stream to gs - so bytes are compressed at
first with GZipStream and then encrypted with CryptoStream

My approach in extracting file from archive is (simplified):

1. read rijndael IV from input file stream (fs)
2. open cryptographic input stream above this stream
CryptoStream cs = new CryptoStream(fs, decryptor, CryptoStreamMode.Read);
3. open gzip input stream above cs
GZipStream gs = new GZipStream(Cs, CompressionMode.Decompress, true);
4. read bytes from this GzipStream to output file stream - so at first I
request some bytes with gs.Read method, then GzipStream requests bytes from

underlying CryptoStream

Adding files into archive works OK. The problem is in extracting, however,
the strange thing is - the decompression fails only sometimes (maybe in 5% of

cases). And exception thrown by Read method is: CryptographicException
"Length of the data to decrypt is invalid.". I am almost sure I have all code

correctly - as I mentioned above, if I deactivate compression or encryption
all works OK, but when I create pipeline of decryption stream and compression

stream, extraction fails sometimes. For example: when I add one file 20x
into archive (20x the same file) and I try extract it, first 10 files are
extracted

OK, extraction of 11th file fails - very strange.

I have a theory, what could be the reason of this. I found a thread of one
developer that maybe relates to my issue ("GZipStream Decompress always reads
4K

minimum...!" -

http://groups.google.sk/group/micro...862f77?lnk=st&q=gzipstream+read&rnum=1&hl=sk#

2f225acf44862f77), which says that GzipStream reads data in 4kB blocks.
Therefore, during my extraction a situation may occur, that I call Read
(number of

bytes < 4k), but GzipStream executes Read (4k bytes) on underlyiing crypto
stream. And, when someone requests more bytes from cryptostream than it can

provide, cryptostream throws an exception. Can this be reason of my problem?
If yes, how can I solve this? I didn't find any solution. One solution can be

decryption of all data into memory followed by decompression of this data -
but I can not use it, because compressed files can be very big.

Thank you for any help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top