1 """Pure-Python RC4 implementation."""
4 from cryptomath import *
10 def __init__(self, key):
11 RC4.__init__(self, key, "python")
12 keyBytes = stringToBytes(key)
13 S = [i for i in range(256)]
16 j = (j + S[i] + keyBytes[i % len(keyBytes)]) % 256
17 S[i], S[j] = S[j], S[i]
23 def encrypt(self, plaintext):
24 plaintextBytes = stringToBytes(plaintext)
28 for x in range(len(plaintextBytes)):
31 S[i], S[j] = S[j], S[i]
32 t = (S[i] + S[j]) % 256
33 plaintextBytes[x] ^= S[t]
36 return bytesToString(plaintextBytes)
38 def decrypt(self, ciphertext):
39 return self.encrypt(ciphertext)