首页 > 代码库 > Python challenge 9 - bz2

Python challenge 9 - bz2

第九题地址:http://www.pythonchallenge.com/pc/def/integrity.html



依旧是图片,我们点击一下会弹出用户名,密码让我们输入,猜测解析之后会得到。继续查看HTML代码。

<!--
un: 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
pw: 'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'
-->

我们看到这串注释,un,pw就是username和password的缩写,那么解析这串注释就能得到通关的密码。

由于是

BZh91AY&SY
开头,根据学习得知是使用了bz2模块进行了加密的结果。


import bz2

un = 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
pw = 'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'

print bz2.decompress(un)
print bz2.decompress(pw)

上述代码我们可以很简单的得到需要的用户名和密码

huge
file



BTW,在参考中看到

<area shape="poly" coords="179,284,214,311,255,320,281,226,319,224,363,309,339,222,371,225,411,229,404,242,415,252,428,233,428,214,394,207,383,205,390,195,423,192,439,193,442,209,440,215,450,221,457,226,469,202,475,187,494,188,494,169,498,147,491,121,477,136,481,96,471,94,458,98,444,91,420,87,405,92,391,88,376,82,350,79,330,82,314,85,305,90,299,96,290,103,276,110,262,114,225,123,212,125,185,133,138,144,118,160,97,168,87,176,110,180,145,176,153,176,150,182,137,190,126,194,121,198,126,203,151,205,160,195,168,217,169,234,170,260,174,282" href=http://www.mamicode.com/"../return/good.html">

这串代码,是点的集合,描述了蜜蜂的形状,鼠标移动上去之后,点击能进入页面跳转。



####################################################################################################################################


我们使用bz2模块来bzip2进行压缩,或加密。compress方法用于压缩数据位一个string,返回一个8-bit的string。如果要得到原始数据,就使用decompress方法。


import bz2

MESSAGE = "the meaning of life"

compressed_message = bz2.compress(MESSAGE)
decompressed_message = bz2.decompress(compressed_message)

print "original:", repr(MESSAGE)
print "compressed message:", repr(compressed_message)
print "decompressed message:", repr(decompressed_message)

# result

original: 'the meaning of life'
compressed message: 'BZh91AY&SY\xcb\x18\xf4\x9e\x00\x00\t\x11\x80@\x00#\xe7\x84\x00 \x00"\x8d\x94\xc3!\x03@\xd0\x00\xfb\xf6U\xa6\xe1p\xb8Z.\xe4\x8ap\xa1!\x961\xe9<'
decompressed message: 'the meaning of life'


bz2模块还提供了BZ2Compressor和BZ2Decompressor类,支持增量的压缩和解压缩。


using the bz2 module for incremental compression

import bz2

text = "the meaning of life"
data = http://www.mamicode.com/"">
压缩不是同时进行的,而是在for loop中分段进行,但是解压的结果还是依旧。



还有BZ2File方法,类似于open方法,可以用于自动读取或者写入压缩文件。


using the bz2 module for stream compression

import bz2

file = bz2.BZ2File("samples/sample.bz2", "r")

for line in file:
    print repr(line)
$ python bz2-example-3.py
'We will perhaps eventually be writing only small\n'
'modules which are identified by name as they are\n'
'used to build larger ones, so that devices like\n'
...



参考:

http://effbot.org/librarybook/bz2.htm

http://pymotw.com/2/bz2/

http://bbs.chinaunix.net/thread-3608204-1-1.html

Python challenge 9 - bz2