python解决唱吧歌词解密的问题?

做唱吧歌词解密的时候选择了语言python,对于字节解码的时候用到了chr函数,但是chr函数参数限制在0 ~ 0xff(255),如果需要chr的值出现负数怎么办呢?我记得php用chr函数的时候支持负数,于是翻阅了一下php的源码,发现php做了一次按位与操作 c &= c,这样一来就不会再出现错误:

Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> chr(-165)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(256)
>>> chr(-165 & 0xff)
'['
>>> 

python解决唱吧歌词解密的问题?

# -*-- coding:utf-8 -*--
import re
import os

class ChangBaDecrypt(object):
    encrypt_key = [-50, -45, 110, 105, 64, 90, 97, 119, 94, 50, 116, 71, 81, 54, -91, -68, ]

    def __init__(self):
        pass

    def decrypt(self, content):
        decrypt_content = bytearray()
        for i in range(0, len(content)):
            var = content[i] ^ self.encrypt_key[i % 16]
            decrypt_content.append(var & 0xff)
        return decrypt_content.decode('utf-8')

    def decrypt_by_file(self, filename):
        with open(filename, 'rb') as f:
            content = f.read()
            f.close()
            decrypt = self.decrypt(content)
            if re.match(r'\[\d+,\d+\]', decrypt):
                return decrypt

changba = ChangBaDecrypt()
decrypt = changba.decrypt_by_file(os.path.join(os.path.curdir, '../tests/data/a89f8523a6724a915c6b2038c928b342.zrce'))
print(decrypt)

0 comments

To reply to the article, please Login or registered