Python: 将字符串转换为八位字节格式

Python: Converting a string to the octet format

我正在尝试在 Python 中实现 OS2IP 算法。但是我不知道如何将字符串(例如 "Men of few words are the best men." )转换为八位字节格式。

使用str.encode()方法。例如:

"öä and ü".encode("utf-8")

显示

b'\xc3\xb6\xc3\xa4 and \xc3\xbc'

如果你想把它转换成一个整数,你可以使用int.from_bytes()方法,例如

the_bytes = "öä and ü".encode("utf-8")
the_int = int.from_bytes(the_bytes, 'big')
print(the_int)

显示

236603614466389086088250300

在准备 RSA 加密时,填充算法通常应用于第一个编码步骤的结果,以将字节数组填充到 RSA 模数的大小,然后将填充的字节数组转换为整数.此填充步骤对于 RSA 加密的安全性至关重要。