简体   繁体   English

Python3:试图将 b' ' 字符串解码为 ascii

[英]Python3: Trying to decode b' ' string to ascii

I'm trying to decrypt a OTP.我正在尝试解密 OTP。 The key is generated with the following code:密钥是使用以下代码生成的:

class OTPGenerator(metaclass=Singleton):
_OTP_LEN = 128

def __init__(self):
    self.otp = os.urandom(OTPGenerator._OTP_LEN)

def get_otp(self):
    return self.otp

I get receive the encrypted text thru a socket connection:我通过套接字连接收到加密文本:

b'7Vf\xba\xe1\xb1.\xeb\x05Y\xccL 1\xb2\xec\xb1<0\xb36\xce\xc3\x02\xd6^\xc6z\x15_\x88\x14k\xe9\x8c\xb1\xa5{\xd5\xe3LKE8\x16\xe2\xe1\xf0\xe1+[_\xd47\x13\xd8T\xa7E\x8f\xf3SR\xd1'

And another encrypted text given by be as plaintext:另一个以明文形式给出的加密文本:

input = flag
Encrypted Input: b'\t\x17J\x9c'

I'm trying to decode them before XOR-ing them, but they are casted as strings.我试图在对它们进行异或运算之前对其进行解码,但它们被转换为字符串。

Has anyone encountered a similar issue before?有没有人遇到过类似的问题?

The os.urandom module produces N random bytes with N being passed as argument. os.urandom模块产生 N 个随机字节,其中 N 作为参数传递。 In that sense, I'm not sure what you want to decode since this is random data.从这个意义上说,我不确定您要解码什么,因为这是随机数据。

In any way, there is this answer that might be what you are looking for, which converts the data produce by os.urandom into a base64 encoded string.无论如何,这可能是您正在寻找的答案,它将os.urandom生成的数据转换为 base64 编码字符串。

from base64 import b64encode
from os import urandom

random_bytes = urandom(64)
token = b64encode(random_bytes).decode('utf-8')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM