gpt4 book ai didi

python - 如何置换 Python 中的位?

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:59 25 4
gpt4 key购买 nike

我已经使用 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 位或更大,更少?

最佳答案

你的方法有很多问题

  1. 您在交易中使用了错误的工具。您可能应该寻找 bitarray
  2. iptable的长度(56)不符合迭代长度(64)

使用位数组校正后的示例代码可能如下所示

>>> 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')

关于python - 如何置换 Python 中的位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com