开发者

purpose of '"sss".decode("base64").decode("zlib")'

开发者 https://www.devze.com 2022-12-15 19:08 出处:网络
ACTIVATE_THIS = \"\"\" eJx1UsGOnDAMvecrIlYriDRlKvU20h5aaY+teuilGo1QALO4CwlKAjP8fe1QGGalRoLEefbzs+Mk Sb7NcvRo3iTcoGqwgyy06As+HWSNVciKaBTFywYoJWc7yit2ndBVwEkHkIzKCV0YdQdmkvShs6YH
ACTIVATE_THIS = """
eJx1UsGOnDAMvecrIlYriDRlKvU20h5aaY+teuilGo1QALO4CwlKAjP8fe1QGGalRoLEefbzs+Mk
Sb7NcvRo3iTcoGqwgyy06As+HWSNVciKaBTFywYoJWc7yit2ndBVwEkHkIzKCV0YdQdmkvShs6YH
E3IhfjFaaSNLoHxQy2sLJrL0ow98JQmEG/rAYn7OobVGogngBgf0P0hjgwgt7HOUaI5DdBVJkggR
3HwSktaqWcCtgiHIH7qHV+esW2CnkRJ+9R5cQGsikkWEV/J7leVGs9TV4TvcO5QOOrTHYI+xeCjY
JR/m9GPDHv2oSZunUokS2A/WBelnvx6tF6LUJO2FjjlH5zU6Q+Kz/9m69LxvSZVSwiOlGnT1rt/A
77j+WDQZ8x9k2mFJetOle88+lc8sJJ/AeerI+fTlQigTfVqJUiXoKaaC3AqmI+KOnivjMLbvBVFU
1JDruuadNGcPmkgiBTnQXUGUDd6IK9JEQ9yPdM96xZP8bieeMRqTuqbxIbbey2DjVUNzRs1rosFS
TsLAdS/0fBGNdTGKhuqD7mUmsFlgGjN2eSj1tM3GnjfXwwCmzjhMbR4rLZXXk+Z/6Hp7Pn2+kJ49
jfgLHgI4Jg==
""".decode("base64").decode("zlib")

my code:

import zlib
print 'dsss'.decod开发者_开发知识库e('base64').decode('zlib')#error

Traceback (most recent call last):
  File "D:\zjm_code\b.py", line 4, in <module>
    print 'dsss'.decode('base64').decode('zlib')
  File "D:\Python25\lib\encodings\zlib_codec.py", line 43, in zlib_decode
    output = zlib.decompress(input)
zlib.error: Error -3 while decompressing data: unknown compression method

a='dsss'.encode('zlib')
print a
a.encode('base64')
print a
a.decode('base64')#error
print a
a.decode('zlib')
print a

x\x9cK)..Traceback (most recent call last):
  File "D:\zjm_code\b.py", line 7, in <module>
    a.decode('base64')
  File "D:\Python25\lib\encodings\base64_codec.py", line 42, in base64_decode
    output = base64.decodestring(input)
  File "D:\Python25\lib\base64.py", line 321, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

a='dsss'
a=a.encode('zlib')
print a
a=a.decode('zlib')
print a#why can't print 'dsss'

x\x9cK)..

a='dsss'
a=a.encode('zlib')
#print a
a=a.decode('zlib')
print a#its ok

i think the 'print a' encode the a with 'uhf-8'. so:

#encoding:utf-8
a='dsss'
a=a.encode('zlib')
print a
a=a.decode('utf-8')#but error.
a=a.decode('zlib')
print a#


x\x9cK)..Traceback (most recent call last):
  File "D:\zjm_code\b.py", line 5, in <module>
    a=a.decode('utf-8')
  File "D:\Python25\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 1: unexpected code byte


The data in the strings is encoded and compressed binary data. The .decode("base64").decode("zlib") unencodes and decompresses it.

The error you got was because 'dsss' decoded from base64 is not valid zlib compressed data.


What is the purpose of x.decode(”base64”).decode(”zlib”) for x in ("sss", "dsss", random_garbage)? Excuse me, you should know; you are the one who is doing it!

Edit after OP's addition of various puzzles

Puzzle 1

a='dsss'.encode('zlib')
print a
a.encode('base64')
print a
a.decode('base64')#error
print a
a.decode('zlib')
print a

Resolution: all 3 statements of the form

a.XXcode('encoding')

should be

a = a.XXcode('encoding')

Puzzle 2

a='dsss'
a=a.encode('zlib')
print a
a=a.decode('zlib')
print a#why can't print 'dsss'

x\x9cK)..

But it does print 'dsss':

>>> a='dsss'
>>> a=a.encode('zlib')
>>> print a
x£K)..♠ ♦F☺¥
>>> a=a.decode('zlib')
>>> print a#why can't print 'dsss'
dsss
>>>

Puzzle 3

"""i think the 'print a' encode the a with 'uhf-8'."""

Resolution: You think extremely incorrectly. What follows the print is an expression. There are no such side effects. What do you imagine happens when you do this:

print 'start text ' + a + 'end text'

?

What do you imagine happens if you do print a twice? Encoding the already-encoded text again? Why don't you stop imagining and try it out?

In any case, note that the output of str.encode('zlib') is an str object, not a unicode object:

>>> print repr('dsss'.encode('zlib'))
'x\x9cK)..\x06\x00\x04F\x01\xbe'

Getting from that to UTF-8 is going to be somewhat difficult ... it would have to be decoded into unicode first -- with what codec? ascii and utf8 are going to have trouble with the '\x9c' and the '\xbe' ...


It is the reverse of:

original_message.encode('zlib').encode('base64')

zlib is a binary compression algorithm. base64 is a text encoding of binary data, which is useful to send binary message through text protocols like SMTP.

After 'dsss' was decoded from base64 (the three bytes 76h, CBh, 2Ch), the result was not valid zlib compressed data so it couldn't be decoded.

Try printing ACTIVATE_THIS to see the result of the decoding. It turns out to be some Python code.


.decode('base64') can be called only on a string that's encoded as "base-64, in order to retrieve the byte sequence that was there encoded. Presumably that byte sequence, in the example you bring, was zlib-compressed, and so the .decode('zlib') part decompresses it.

Now, for your case:

>>> 'dsss'.decode('base64')
'v\xcb,'

But 'v\xcv,' is not a zlib-compressed string! And so of course you cannot ask zlib to "decompress" it. Fortunately zlib recognizes the fact (that 'v\xcv,' could not possibly have been produced by applying any of the compression algorithms zlib knows about to any input whatsoever) and so gives you a helpful error message (instead of a random-ish string of bytes, which you might well have gotten if you had randomly supplied a different but equally crazy input string!-)

Edit: the error in

a.encode('base64')
print a
a.decode('base64')#error

is obviously due to the fact that strings are immutable: just calling a.encode (or any other method) does not alter a, it produces a new string object (and here you're just printing it).

In the next snippet, the error is only in the OP's mind:

>>> a='dsss'
>>> a=a.encode('zlib')
>>> print a
x?K)..F?
>>> a=a.decode('zlib')
>>> print a#why can't print 'dsss'
dsss
>>> 

that "why can't print" question is truly peculiar, applied to code that does print 'dsss'. Finally,

i think the 'print a' encode the a with 'uhf-8'.

You think wrongly: there's no such thing as "uhf-8" (you mean "utf-8" maybe?), and anyway print a does not alter a, any more than just calling a.encode does.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号