0

I have this very simple Python code:

in_data = "eNrtmD1Lw0AY..."
print("Input: " + in_data)
out_data = in_data.decode('base64').decode('zlib').encode('zlib').encode('base64')
print("Output: " + out_data)

It outputs:

Input: eNrtmD1Lw0AY...
Output: eJztmE1LAkEY...

The string is also correctly decoded; if I display in_data.decode('base64').decode('zlib'), it gives the expected result.

Also, the formatting is different for both strings:

Weird formatting

Why is the decoding/encoding not working properly? Are there some sort of parameters I should use?

pie3636
  • 795
  • 17
  • 31
  • 1
    The formatting follows standard base64 rules; newlines are permitted and preferred at 76 columns. Perhaps your input data used a heavier or lighter compression setting? – Martijn Pieters May 29 '16 at 18:08
  • Please include the *full input string* so we can properly diagnose. – Martijn Pieters May 29 '16 at 18:09
  • Here it is: http://pastebin.com/LUy2Ybs4 – pie3636 May 29 '16 at 18:10
  • 1
    Seeing as the `N` and `J` are what differs first, I'd say this is a [different compression setting](http://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like). You have 78DA on the input, 789C on your output. That's a different, lower compression ratio (default). – Martijn Pieters May 29 '16 at 18:13

1 Answers1

2

Your data on input starts with the hex bytes 78 DA, your output starts with 78 9C:

>>> 'eNrt'.decode('base64').encode('hex')[:4]
'78da'
>>> 'eJzt'.decode('base64').encode('hex')[:4]
'789c'

DA is the highest compression level, 9C is the default. See What does a zlib header look like?

Rather than use .encode('zlib') use the zlib.compress() function, an set the level to 9:

import zlib

zlib.compress(decoded_data, 9).encode('base64')

The output of the base64 encoding inserts a newline every 76 characters to make it suitable for MIME encapsulation (emailing). You could use the base64.b64encode() function instead to encode without newlines.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343