我有一个密码学课程,我将解密RSA-Chipher。现在,当解密完成后,我希望将解密列表(解密[])中的每个数字转换为字符,以便可以读取文本。
在解密列表[0]中,我有138766326357071967404457122245626646062。我应该如何将此数字转换为可读文本?
我尝试从字符串转到int:
plainText = "stackoverflow".encode('hex')
plainInt = long(plainText,16)
print plainInt
=> 9147256685580292608768854486903
现在我想从plainint到" stackoverflow"。有什么技巧我应该如何实现这一目标?
这适用于python 2和3
import codecs
b = hex(plainInt).rstrip("L").lstrip("0x")
codecs.decode(b, 'hex').decode('utf-8')
在python 2中,您可以对将字符串转换为数字的倒数:
:>>> plainHex = hex(plainInt)[2:-1]
>>> plainHex.decode('hex')
'stackoverflow'
在Python 3中,INT具有" to_bytes
"函数,该功能采用字节长度和字节顺序(大或小ENDIAN):
>>> plainInt.to_bytes(13, byteorder='big')
b'stackoverflow'
回答您的示例:使用 hex
从长到十六进制和 decode
倒退以从十六进制:
>>> plain_hex = hex(plainInt)
>>> print plain_hex
0x737461636b6f766572666c6f77L
>>> str(plain_hex)[2:-1].decode('hex')
'stackoverflow'