concatenation of two or more base64 strings in python

元气小坏坏 提交于 2021-02-20 06:50:58

问题


I'm tring to concatenate two strings encoded to base64 but it doesn't really work, just prints the first string in concatanation:

q = base64.b64encode("StringA")
print q # prints an encoded string
q = q+base64.b64encode("StringB")
print q # prints an encoded string

print base64.b64decode(q) # just prints "StringA"

回答1:


You are decoding a string that is concatenation of two base64 strings. This is not correct. You should do something like this -

base64.b64decode(base64.b64encode("StringA" + "StringB"))



回答2:


This works:

>>> ''.join([base64.b64encode("StringA"), base64.b64encode("StringB")])
'U3RyaW5nQQ==U3RyaW5nQg=='


来源:https://stackoverflow.com/questions/37278521/concatenation-of-two-or-more-base64-strings-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!