Python 将整数转换为 16 字节字节
Python convert integer to 16-byte bytes
我正在尝试在我的代码中使用 AES-CTR-128。我使用 Python 2.7 并利用 cryptography
模块进行加密。
我需要设置特定的计数器值,例如 counter_iv = 112。当我尝试
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
key = os.urandom(32)
counter_iv = 112
cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(b"a secret message") + encryptor.finalize()
这给了我一条错误信息:
Traceback (most recent call last):
File "aestest.py", line 14, in <module>
cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend)
File "/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/ciphers/modes.py", line 139, in __init__
raise TypeError("nonce must be bytes")
TypeError: nonce must be bytes
我认为它告诉我 counter_iv 应该是一个 16 字节的字符串。
我的问题是如何将整数或长整数转换为 16 字节的字符串?
另外,有什么方法可以将整数转换为任意长度的字符串吗?感谢您的帮助。
在Python2中,你可以像这样将整数转换为小端字节串:
counter_iv = 112 # This can be a pretty big number
iv_bytes = "".join(chr((counter_iv >> (i * 8)) & 0xFF) for i in range(16))
展开代码说明:
counter_iv = 112 # Can be from 0 to 3.40e38
temp = [] # Will be an array of bytes
for i in range(16):
# Get the i'th byte counting from the least significant end
b = (counter_iv >> (i * 8)) & 0xFF
temp.append(b)
# For example, temp == [0x70, 0x00, ... 0x00]
# Will be an array of 1-character strings
temp2 = [chr(b) for b in temp]
# For example, temp2 == ['\x70', '\x00', ..., '\x00']
# Concatenate all the above together
iv_bytes = "".join(temp2)
# For example, iv_bytes == '[=11=]x70[=11=]x00...[=11=]x00'
首先,您是否有充分的理由使用低级别 危险 material 而不是较高级别 API?此级别旨在仅通过高级 API 用于特殊目的。
接下来,您通过编写 modes.CTR(counter_iv)
来进一步误导 reader,因为 counter 模式不使用初始化向量,而是使用 nonce根据其documentation。你得到的错误是正常的,因为文档指出随机数应该是一个字节字符串与密码块大小相同所以对于AES它必须是128位或16字节。
顺便说一句,该文档还指出永远不应重复使用随机数
... It is critical to never reuse a nonce with a given key. Any reuse of a nonce with the same key compromises the security of every message encrypted with that key.
Nayuki 的回答解释了如何从您的 int 构建一个 16 字节的字符串,但是如果您使用低级别 hazardous material[=,请务必正确使用密码术13=]
我正在尝试在我的代码中使用 AES-CTR-128。我使用 Python 2.7 并利用 cryptography
模块进行加密。
我需要设置特定的计数器值,例如 counter_iv = 112。当我尝试
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
key = os.urandom(32)
counter_iv = 112
cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(b"a secret message") + encryptor.finalize()
这给了我一条错误信息:
Traceback (most recent call last): File "aestest.py", line 14, in <module> cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend) File "/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/ciphers/modes.py", line 139, in __init__ raise TypeError("nonce must be bytes") TypeError: nonce must be bytes
我认为它告诉我 counter_iv 应该是一个 16 字节的字符串。
我的问题是如何将整数或长整数转换为 16 字节的字符串?
另外,有什么方法可以将整数转换为任意长度的字符串吗?感谢您的帮助。
在Python2中,你可以像这样将整数转换为小端字节串:
counter_iv = 112 # This can be a pretty big number
iv_bytes = "".join(chr((counter_iv >> (i * 8)) & 0xFF) for i in range(16))
展开代码说明:
counter_iv = 112 # Can be from 0 to 3.40e38
temp = [] # Will be an array of bytes
for i in range(16):
# Get the i'th byte counting from the least significant end
b = (counter_iv >> (i * 8)) & 0xFF
temp.append(b)
# For example, temp == [0x70, 0x00, ... 0x00]
# Will be an array of 1-character strings
temp2 = [chr(b) for b in temp]
# For example, temp2 == ['\x70', '\x00', ..., '\x00']
# Concatenate all the above together
iv_bytes = "".join(temp2)
# For example, iv_bytes == '[=11=]x70[=11=]x00...[=11=]x00'
首先,您是否有充分的理由使用低级别 危险 material 而不是较高级别 API?此级别旨在仅通过高级 API 用于特殊目的。
接下来,您通过编写 modes.CTR(counter_iv)
来进一步误导 reader,因为 counter 模式不使用初始化向量,而是使用 nonce根据其documentation。你得到的错误是正常的,因为文档指出随机数应该是一个字节字符串与密码块大小相同所以对于AES它必须是128位或16字节。
顺便说一句,该文档还指出永远不应重复使用随机数
... It is critical to never reuse a nonce with a given key. Any reuse of a nonce with the same key compromises the security of every message encrypted with that key.
Nayuki 的回答解释了如何从您的 int 构建一个 16 字节的字符串,但是如果您使用低级别 hazardous material[=,请务必正确使用密码术13=]