所以我正在寻找python bytes()函数的一些解决方法,因为它只允许最多256个。这段代码:
def mask(rawBytes):
data = [ord(i) for i in rawBytes]
length = len(rawBytes) + 128
Bytes = [0x81, length]
index = 2
mask = os.urandom(4)
for i in range(len(mask)):
Bytes.insert(i + index, mask[i])
for i in range(len(data)):
data[i] ^= mask[i % 4]
Bytes.insert(i + index + 4, data[i])
return bytes(Bytes)
屏蔽我需要的数据。但是,如果尝试屏蔽大字符串,则会发生这种情况:
>>> mask('i'*1000)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
mask('i'*1000)
File "C:/Python33/cakelibw.py", line 546, in mask
return bytes(Bytes)
ValueError: bytes must be in range(0, 256)
>>>
现在有办法解决这个问题吗?或另一种允许大字符串的方法?
答案 0 :(得分:1)
def mask(rawBytes):
data = [ord(i) for i in rawBytes]
length = len(rawBytes) + 128 if len(rawBytes) + 128 <= 254 else 254
Bytes = [0x81, length]
index = 2
mask = os.urandom(4)
for i in range(len(mask)):
Bytes.insert(i + index, mask[i])
for i in range(len(data)):
data[i] ^= mask[i % 4]
Bytes.insert(i + index + 4, data[i])
return bytes(Bytes)