将大数转换为chars Python

2024-10-01 13:43:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一门密码学的课程,我要解密一个RSA芯片。现在解密完成后,我想把解密列表(decryptList[])中的每个数字转换成字符,这样文本就可读了。在

在解密列表[0]中,我有138766332635707196740445712245626646062。我该怎么把这个数字转换成可读的文本?在

我试着从字符串变为int:

plainText = "stackoverflow".encode('hex')
plainInt = long(plainText,16)
print plainInt
=> 9147256685580292608768854486903

现在我想从plainInt转到“stackoverflow”。 有什么小贴士可以帮我完成这个任务吗?在


Tags: 字符串文本列表数字字符stackoverflowrsa芯片
3条回答

在Python2中,可以执行与将字符串转换为数字相反的操作:

>>> plainHex = hex(plainInt)[2:-1]
>>> plainHex.decode('hex')
'stackoverflow'

在python3中,int有一个“to_bytes”函数,它采用字节长度和字节顺序(big或littleendian):

^{pr2}$

回答您的示例:使用hex从long向后返回到hex,使用{}从hex获取字符串:

>>> plain_hex = hex(plainInt)
>>> print plain_hex
0x737461636b6f766572666c6f77L
>>> str(plain_hex)[2:-1].decode('hex')
'stackoverflow'

这适用于Python2和3

import codecs
b = hex(plainInt).rstrip("L").lstrip("0x")
codecs.decode(b, 'hex').decode('utf-8')

相关问题 更多 >