我编写了以下代码来使用Python代码置换位:
iptable=[56,48,40,32,24,16,8,0,57,49, 41,33,25,17,9,1, 58, 50, 42, 34, 26,
18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14,
6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3]
msg= b'AABBCCDD'
enc = ''
i = 0
while i <= 63:
x = iptable[i]
enc = enc + msg[x]
i = i + 1
print(enc)
它返回了
enc = enc + msg[x]
IndexError: index out of range
那么错误在哪里?并且msg变量中的每个字符是否转换为8位或更大,更少?
答案 0 :(得分:3)
您的方法存在很多问题
使用bitarray的示例更正代码可能如下所示
>>> iptable=[56,48,40,32,24,16,8,0,57,49, 41,33,25,17,9,1, 58, 50, 42, 34, 26,
18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14,
6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3]
>>> from bitarray import bitarray
>>> msg = bitarray(endian='little')
>>> msg.frombytes('AABBCCDD')
>>> enc = bitarray(endian='little')
>>> for i in iptable:
enc.append(msg[i])
>>> enc
bitarray('00110011001111001100000000001111111100000000000000000000')
>>> msg
bitarray('1000001010000010010000100100001011000010110000100010001000100010')
答案 1 :(得分:0)
您可以通过将消息转换为整数来置换位:
def tobin(n):
return bin(n)[2:].zfill(len(msg)*8)
msgint = int.from_bytes(msg, byteorder='big')
print(tobin(msgint))
print(msgint.to_bytes(len(msg), byteorder='big', signed=False))
# -> 0100000101000001010000100100001001000011010000110100010001000100
# -> b'AABBCCDD'
然后你可以直接执行位操作:
result = 0
for i, j in enumerate(iptable):
if msgint & (1 << j): # if j-th bit set in msg
result |= (1 << i) # set i-th bit in the result
print(tobin(result))
print(result.to_bytes(len(msg), byteorder='big', signed=False))
# -> 0000000000000000000000000000111111110000110000000011110000110011
# -> b'\x00\x00\x00\x0f\xf0\xc0<3'