我正在用 Python 编写一个程序,它将获取一段二进制文件并根据密钥对其进行加密。如果片段比密钥短,它必须取密钥的最后一位,因此是双重反转。我的问题是在尝试将最后一段键添加到空白列表时,它给了我“列表索引超出范围”错误。代码如下:
def OTP(msg,clave):
encList = []
encr = ''
clist = list(clave)
clist.reverse()
cutlist = []
mlist = list(msg)
mlist.reverse()
for i in range(len(msg)):
cutlist.append(clist[i])
for i in mlist:
for j in cutlist:
if i == j:
encList.append(0)
if 1 != j:
encList.append(1)
encList.reverse()
for i in encList:
encr += str(encList[i])
return encr
clave = '000000010011010010001001000110110011001101010011100101010000101100111110000010100000011010010000101100000101100011010110100000100110001011001101101110110101000010000010100101000101101101010010001100001100100010111111111110010011101110010101110100111110000001101111110010000000101011000101111110100100101000110010111001100110011010100011011001101010011111100101'
msg = '01101000011010000110100001101000'
cript = OTP(msg,clave)
rev = OTP(cript,clave)
print(rev)
我给它消息长度的范围,它应该在更长的键的范围内。与往常一样,我们将不胜感激任何帮助。