自动解码以base64格式编码的反向多行字符串

时间:2012-12-13 15:01:45

标签: python base64 decoding

我正在解码下面给出的以base64格式编码的反向多行字符串。

LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ = ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg

s = 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg'
s = s.decode('base64', 'strict')

print s[::-1]

通过上面的方法,我得到了正确的字符串,但我必须手动更改字符串。

请帮忙。

1 个答案:

答案 0 :(得分:2)

来自OP的评论:

list = ['LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ=', 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg', 'c25vaXRhY2lmaWNlcHMgOC1QRVAgdGVlbSB0c3VNICl2ICAgIA==', 'Ni4yIG5vaHR5UCBodGl3IGtyb3cgdHN1TSApdmkgICAg', 'c2VsdWRvbSByZWh0byB5YiBlbGJhdHJvcG1pIGViIGRsdW9ocyBzc2FsQyApaWlpICAgIA==']
s = ''.join(list)
s = s.decode('base64', 'strict')
print (s[::-1])

join操作将所有字符串连接在一起,但只翻译了第一个字符串。这是因为,当解码基本64字符串时,将忽略字符串中超过前一个或两个=字符的所有内容。而不是加入,迭代:

list = ['LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ=', 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg', 'c25vaXRhY2lmaWNlcHMgOC1QRVAgdGVlbSB0c3VNICl2ICAgIA==', 'Ni4yIG5vaHR5UCBodGl3IGtyb3cgdHN1TSApdmkgICAg', 'c2VsdWRvbSByZWh0byB5YiBlbGJhdHJvcG1pIGViIGRsdW9ocyBzc2FsQyApaWlpICAgIA==']
for s in list:
    s = s.decode('base64', 'strict')
    print (s[::-1])

或使用列表理解。

list = ['LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ=', 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg', 'c25vaXRhY2lmaWNlcHMgOC1QRVAgdGVlbSB0c3VNICl2ICAgIA==', 'Ni4yIG5vaHR5UCBodGl3IGtyb3cgdHN1TSApdmkgICAg', 'c2VsdWRvbSByZWh0byB5YiBlbGJhdHJvcG1pIGViIGRsdW9ocyBzc2FsQyApaWlpICAgIA==']
print "\n".join(s.decode('base64')[::-1] for s in list)

输出:

4. Describe the concept of denormalisation on high traffic websites. Feel free to use examples.
    vi) Must be in one file
    v) Must meet PEP-8 specifications
    iv) Must work with Python 2.6
    iii) Class should be importable by other modules