Using compression with encryption

Tech-Archive recommends: Fix windows errors by optimizing your registry



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/microsoft.public.dotnet.framework/browse_thread/thread/e317e6249146e7dd/2f225acf44862f77?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.
.



Relevant Pages

  • AidAim CryptoPressStream 2.00
    ... CryptoPressStream is a streaming compression and encryption library. ... in the stream object. ...
    (comp.software.shareware.announce)
  • Using compression with encryption
    ... I am developing an internal compression format. ... encryption as well. ... open cryptographic output stream above this stream ... first with GZipStream and then encrypted with CryptoStream ...
    (microsoft.public.dotnet.framework)
  • Re: typedef struct _iobuf FILE;
    ... You can also implement a "FILE *" stream that sits atop a compression ... algorithm, or a "FILE *" stream that implements encryption, or ... Reading email is like searching for food in the garbage, ...
    (comp.lang.c)
  • [ANN] CryptoPressStream - new streaming compression and encryption library
    ... CryptoPressStream is a streaming compression and ecnryption library. ... It provides transparent access to compressed or encrypted data stored in the stream object. ... - Transparent streaming compression and encryption library with 100 % TStream compatibility. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: BinaryWriter.Flush and BufferedStream.Flush
    ... One stream is encrypted, ... RSACryptoServiceProvider using both its encryption and signature ... >> The CryptoStream is broken, at least when used with Rijndael in CBC ... >> flush its own unfinished buffer it should at least pass the flush on to ...
    (microsoft.public.dotnet.framework.performance)