Re: IO.Compression and Encryption

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



AnikSol <anisol@xxxxxxxxxxxxxxxx> wrote:

<snip>

Can anyone point out what are we doing wrong? If we just compress and
decompress the file using the same code, its perfect. If we encrypt and
decrypt the file using the same class, its perfect. But if we do the
combination of compression and encryption, the file is totally messed up.
Any reasons?

Clearly the encryption/decryption is *not* valid on its own - that much
is clear from the fact that you can't decrypt the encrypted data and
get the same size.

By the looks of it, the problem is that you're using a StreamReader on
the encrypted file. StreamReaders deal with *text*, not arbitrary
binary data - and compressed data is effectively arbitrary binary data.
I'm sure you'll find that the encryption/decryption fails on its own if
you try to encrypt/decrypt a binary file like an image.

You should write the decrypted data by reading a chunk from the
decryption stream, writing it to the file stream, then reading a chunk,
etc, until you've read everything.

See http://www.pobox.com/~skeet/csharp/readbinary.html for more on
this.

At a quick glance, it looks like your compression is assuming text data
as well: it shouldn't. Compression and encryption should generally work
on just binary data. That way *any* file should come through correctly,
whether it's text or not.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: SMIME Decryption
    ... EnvelopedCms, Decode it with the encrypted binary data and then Decrypt it. ... " Encryption alg=", ...
    (microsoft.public.dotnet.security)
  • Re: DES Encryption and Decryption using different keys gives Bad Data exception
    ... The DESCryptoServiceProvider is based on Symmetric encryption algorithm. ... To decrypt the data the user must possess the same key and IV ... Dim btKEK1() As Byte ... 'Create the encryption stream using the memory stream and the ...
    (microsoft.public.dotnet.security)
  • How do I Use DPAPI to Encrypt and Decrypt Data (C#/VB.NET)?
    ... Use DPAPI to Encrypt and Decrypt Data ... The code below demonstrates how to call Data Protection API (DPAPI) ... In addition to encryption and decryption, ... public static string Encrypt ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Byte array to string and back - newbie question
    ... // Create a symmetric algorithm. ... This is done to make encryption more ... // Encrypt a string into a string using a password ... // Decrypt a byte array into a byte array using a key and an IV ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: CryptAPI(encryption/decryption)
    ... It seems like you're missing the Base64 decode step when trying to decrypt ... I misspelled the Private Key as Primary Key. ... Is there any variation in the encryption format in openssl compared to ... "Dylan DSilva " wrote: ...
    (microsoft.public.pocketpc.developer)