如何为python 3实现RC4加密?
我的代码引发此错误:如何为python 3实现RC4加密?
Error : j = (j + S[i] + k[i % keyLen]) % 256
IndexError: list index out of range
我的代码:
#!/usr/bin/python3key = 242
def main():
initializeS(key)
#Key-Scheduling algorithm used to initialize a permutation in array S
#Keylength is the number of bytes in the key it can vary from 0 - 256 bytes
def initializeS(k):
k = str(k).encode('utf-8')
print(k)
keyLen = len(k)
S = []
j = 0
for i in range(0, 256):
j = (j + S[i] + k[i % keyLen]) % 256
S[i], S[j] = S[j], S[i]
return S
main()
回答:
S被初始化为空列表,所以当S[i]
执行该行上,它会因为列表中有0失败元素。将S初始化为至少包含256个元素的列表,因此S[i]
对于[0,256)之间的整数i始终有效,并且不会再出现错误。
以上是 如何为python 3实现RC4加密? 的全部内容, 来源链接: utcz.com/qa/266213.html