1. Hot New Devices! HTC One M9 | Samsung Galaxy S6 | LG G4

Decompress a GZIP "string" compressed in .NET


  1. AndPig

    AndPig New Member

    Hallo,

    I've got an issue trying to decompress something that was compressed in VB.NET using Gzip.

    In .NET the following occurs to an XML file:

    - The file is compressed using Gzip
    - The result is encrypted using Triple3DES
    - The result is saved to a file

    In Android, I've got the 3DES decryption working fine, which when I open a file where I only encrypted in .NET, returns the XML that was originally encrypted.

    The problem is trying to get the decompress function working also. I think the issue relates to the compressed data being binary and not a string, but only a hunch.

    This is my decrypt function - I use fileToByteArray to convert the file on the SD card to pass in as 'message':

    Code (Text):
    1. public static String decrypt(byte[] message) throws Exception {
    2.         final byte[] keyBytes = "1232223423513131".getBytes();
    3.         final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    4.         final IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
    5.         final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS7Padding");
    6.         decipher.init(Cipher.DECRYPT_MODE, key, iv);
    7.  
    8.         final byte[] plainText = decipher.doFinal(message);
    9.  
    10.         return new String(plainText);
    11.     }

    The above works fine when I open a file that was only encrypted in .NET. But if I try to decompress the resultant string with the below, I get an "Unknown format (magic number 8)" error. Note, I've tried about 10 different decompress functions I've found online, all do the same:

    Code (Text):
    1. public static String decompress(String gzipString) throws IOException {
    2.         int size = 0;
    3.        
    4.         byte[] gzipBuff = gzipString.getBytes();
    5.      
    6.         ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff, 4,
    7.                 gzipBuff.length - 4);
    8.         GZIPInputStream gzin = new GZIPInputStream(memstream);
    9.  
    10.         final int buffSize = 8192;
    11.         byte[] tempBuffer = new byte[buffSize];
    12.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    13.         while ((size = gzin.read(tempBuffer, 0, buffSize)) != -1) {
    14.             baos.write(tempBuffer, 0, size);
    15.         }
    16.         byte[] buffer = baos.toByteArray();
    17.         baos.close();
    18.  
    19.         return new String(buffer, "UTF-8");
    20.     }
    The gzipString parameter value passed in to the decompress function looks like binary would as a string.. it contains lots of odd characters that won't paste into here. Not sure if that helps!

    Can anyone suggest what might be wrong, or a good path for investigating this further?

    Any help appreciated.

    Thanks.

    Advertisement
  2. sfdfesfs

    sfdfesfs New Member

    So hard.
  3. jonbonazza

    jonbonazza Well-Known Member

    Where exactly is it failing? Is it crashing or just returning null/empty string? If it's crashing, can you post the logcat output? Otherwise, I would recommend throwing in some log statements to find out exactly what line is causing issues.

Share This Page