CryptoStream - GZipStream, Possible???

  • Thread starter Thread starter Martin Madreza
  • Start date Start date
M

Martin Madreza

Hi,

i've got problems with CryptoStream and GZipStream. Someone tried that? Or
got any idea why this wont work?

i tried every combination, first i used CryptoStream, after that GZipStream.
Or first i create a file with GZipStream and crypt that file. But become back
the orginal is impossible...

I hope anyone got an idea how this could work.

Thanks

Martin
 
They should work fine together... just make sure you gzip first, then
encrypt (encrypted data doesn't compress). What error (etc) did you
see?

Marc
 
Like so:

Rijndael r = Rijndael.Create();
Console.WriteLine("IV: " + r.IV);
Console.WriteLine("Key: " + r.Key);
using(FileStream raw = File.Create("foo.bar"))
using(CryptoStream cs = new CryptoStream(raw,
r.CreateEncryptor(), CryptoStreamMode.Write))
using(GZipStream gzip = new GZipStream(cs,
CompressionMode.Compress))
using(StreamWriter writer = new StreamWriter(gzip)) {
for(int i = 0 ; i < 100 ; i++) {
writer.WriteLine("Line {0} - blah blah blah blah",
i);
}
writer.Close();
gzip.Close();
cs.Close();
raw.Close();
}

Console.WriteLine("Size: " + new
FileInfo("foo.bar").Length);

using(FileStream raw = File.OpenRead("foo.bar"))
using(CryptoStream cs = new CryptoStream(raw,
r.CreateDecryptor(), CryptoStreamMode.Read))
using(GZipStream gzip = new GZipStream(cs,
CompressionMode.Decompress))
using (StreamReader reader = new StreamReader(gzip))
{
string line;
while((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
 

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

Back
Top