虽然我被要求使用它来实现RC4算法,但我的Python经验有限。我有以下代码似乎正确然而生成错误
'range' object does not support item assignment
为该行:
S[i], S[j] = S[j], S[i] # swap
这是我的代码:
def KSA(key):
keylength = len(key)
S = range(256)
j = 0
for i in S:
j = (j + S[i] + key[i % keylength]) % 256
S[i], S[j] = S[j], S[i] # swap
return S
答案 0 :(得分:1)
在Python 3 range
语句中返回一个生成器对象,而不是列表。从
S = range(256)
到
S = list(range(256))