【问题标题】:base64 decode and byte negotiate stringbase64 解码和字节协商字符串
【发布时间】:2019-10-12 09:17:57
【问题描述】:

我有 base64 编码的字符串sZCLmg==,即Note。我想做的是用base64解码它,然后使用字节协商来获取字符串Note

import base64
encoded = 'sZCLmg==' #sZCLmg==  Note
data = base64.b64decode(encoded)
print data

mylist = []
mylist.append(data)
#print mylist[0][0]
bytes = mylist[0][0]
print (bytes ^ 0xFF)

但我收到错误:ValueError: invalid literal for int() with base 10: '\xb1' 任何想法请我做错什么来获取原始字符串Note

【问题讨论】:

  • 你确定这是正确的编码字符串吗?我收到了Tm90ZQ== Note
  • @TheGamer007,显然字节在 Base64 编码之前被否定了。

标签: python python-2.7 decode


【解决方案1】:

在 Python2 中,迭代文字 '\xb1\x90\x8b\x9a' 会生成字符串,而不是字节。

一种解决方案是使用bytearray

>>> ba = bytearray(data)
>>> ''.join(chr(x ^ 0xFF) for x in ba)
'Note'

正如@wovano 在 cmets 中指出的那样,也可以在不使用字节数组的情况下执行此操作,如下所示:

''.join(chr(ord(x) ^ 0xFF) for x in data) 

【讨论】:

  • 是的,它会产生字符串,不能与 0xFF 异或。但是没有必要使用bytearray。以下给出了相同的结果:''.join(chr(ord(x) ^ 0xFF) for x in data).
猜你喜欢
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
相关资源
最近更新 更多