Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
370 views
1
# key-scheduling algorithm
2
3
def KSA(key):
4
5
keylength = len(key)
6
S = range(256)
7
j = 0
8
9
for i in range(256):
10
j = (j + S[i] + key[i % keylength]) % 256
11
S[i], S[j] = S[j], S[i] # swap
12
return S
13
14
# pseudo-random generation algorithm
15
16
def PRGA(S):
17
18
i = 0
19
j = 0
20
while True:
21
i = (i + 1) % 256
22
j = (j + S[i]) % 256
23
S[i], S[j] = S[j], S[i] # swap
24
K = S[(S[i] + S[j]) % 256]
25
yield K
26
27
def RC4(key):
28
29
def convert_key(s):
30
return [ord(c) for c in s]
31
key = convert_key(key)
32
33
S = KSA(key)
34
return PRGA(S)
35