Flow decompression

I am trying to unzip a stream using GZipStream and BinaryStream, but I fail.

Can you help me?

public static LicenseOwnerRoot GetLicenseFromStream(Stream stream) { using (BinaryReader br = new BinaryReader(stream)) { string keyCrypto = br.ReadString(); string xmlCrypto = br.ReadString(); string key = Cryptography.Decrypt(keyCrypto); string xml = Cryptography.Decrypt(key, xmlCrypto); byte[] data = Encoding.UTF8.GetBytes(xml.ToCharArray()); using (MemoryStream ms = new MemoryStream(data)) { using (GZipStream decompress = new GZipStream(ms, CompressionMode.Decompress)) { xml = Encoding.UTF8.GetString(data); LicenseOwnerRoot root = (LicenseOwnerRoot)Utility.XmlDeserialization(typeof(LicenseOwnerRoot), xml); foreach (LicenseOwnerItem loi in root.Licenses) loi.Root = root; return root; } } } } 

This xml is compressed and encrypted, so I need to unzip and then decrypt. When I try to read, you throw one of the expectations with this message: The magic number in the GZip header is incorrect. I tried many times to fix it, but it sounds workable. The question is, how should I use the โ€œhabitsโ€, and if so, or is there another way to do what I'm trying to do? Should I unzip first to use BinaryReader?

Actually, I need to reverse this method:

  public static void GenerateLicenseStream(string key, LicenseOwnerRoot root, Stream stream) { using (BinaryWriter sw = new BinaryWriter(stream)) { string xml = Utility.XmlSerialization(root); using (MemoryStream ms = new MemoryStream()) { using (GZipStream compress = new GZipStream(ms, CompressionMode.Compress)) { byte[] data = Encoding.UTF8.GetBytes(xml.ToCharArray()); compress.Write(data, 0, data.Length); string keyCrypto = Cryptography.Encrypt(key); string xmlCrypto = Cryptography.Encrypt(key, Encoding.UTF8.GetString(ms.ToArray())); sw.Write(keyCrypto); sw.Write(xmlCrypto); } } } } 
+4
source share
3 answers

I wrote you a quick example, it does not do encryption, but it emphasizes where the encryption / decryption should take place. This is the contents of the .NET Console application that you can run "as is":

  static void Main(string[] args) { var content = @"someTextOrXMLContentGoesHereCanBeAnything#$&%&*(@#$^"; var data = Encoding.UTF8.GetBytes(content.ToCharArray()); var fs = new StreamWriter(@"c:\users\stackoverflow\desktop\sample.bin"); using (var bw = new BinaryWriter(fs.BaseStream)) { using (var ms = new MemoryStream()) { using (var compress = new GZipStream(ms, CompressionMode.Compress, true)) { compress.Write(data, 0, data.Length); } // encrypt goes here var compressedData = ms.ToArray(); Console.WriteLine(compressedData.Length); // 179 bw.Write(compressedData); } } // and the reverse... using (var fs2 = new StreamReader(@"c:\users\stackoverflow\desktop\sample.bin")) { using (var br = new BinaryReader(fs2.BaseStream)) { var len = (int)br.BaseStream.Length; var encrypted = br.ReadBytes(len); // decrypt here var decrypted = encrypted; // <== new result after decryption using (var ms = new MemoryStream(decrypted)) { List<byte> bytesList = new List<byte>(); using (var decompress = new GZipStream(ms, CompressionMode.Decompress, true)) { int val = decompress.ReadByte(); while (val > -1) { bytesList.Add((byte)val); val = decompress.ReadByte(); } } var final_result = new String(Encoding.UTF8.GetChars(bytesList.ToArray())); Console.WriteLine(final_result); } } } Console.ReadLine(); } 
+2
source

You treat compressed data as an utf-8 byte array. Utf-8 actually has very strict rules, so half of this compressed data is probably replaced by question marks (placeholders for an invalid character) in this step.

You need to encrypt / decrypt the raw binary data and lose the string conversion. Compressed data is not a string and should not be treated as such.

If your encryption method can only work with strings (I have no definition of your Cryptography class), then you have no choice but to encrypt the XML data first and then compress it (although it probably won't compress this path either).

You also do not decompress; you create a MemoryStream and GZipStream for compressed data, but then do nothing with them and try to use data directly.

+2
source

You donโ€™t read anything from unboxing. You need to read all the data from the โ€œdecompressionโ€ (since the length of the data you have to read until the stream is full) and convert it to a string, as you do, is not required.

+2
source

All Articles