Just thought I'd post this because I can.
Python 2.6 compliant
Code:
import struct
class RC4:
def __init__(self, InitializeKeyStream = []):
#Place the keystream in the following array
#The following is just an example of a keystream
if InitializeKeyStream == []:
self.keystream = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]
else:
self.keystream = InitializeKeyStream
self.state = range(256)
length = len(self.keystream)
x = 0
for i in range(256):
#Randomization step
x = (self.keystream[i % length] + self.state[i] + x) & 0xFF
#Swap the array index i and x in the "state" array
self.state[i], self.state[x] = (self.state[x], self.state[i])
def crypt(self, string):
#Create an array filled with 0's the same size as the length
#of the string
output = [0] * len(string)
#Change the string into an array of numbers
string = struct.unpack('%dB' % len(string), string)
#Copy the keystream over to a local variable
keys = [i for i in self.state]
#Begin PRGA
x = 0 ; y = 0
for i, character in enumerate(string):
x = (x + 1) & 0xFF
y = (y + keys[x]) & 0xFF
keys[x], keys[y] = (keys[y], keys[x])
output[i] = character ^ keys[(keys[x] + keys[y]) & 0xFF]
#Change the numbers into a string
return struct.pack('%dB' % len(output), *output)
Here's how to use it in Python:
>>> Crypto = RC4() #Create instance
>>> EncryptedText = Crypto.crypt("Hello, World!")
>>> DecryptedText = Crypto.crypt(EncryptedText)
>>> print DecryptedText
Hello, World!
Or, if you want, you can choose to define the keys to use in the algorithm.
>>> Crypto = RC4([0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77])
>>> EncryptedText = Crypto.crypt("Hello, World!")
>>> DecryptedText = Crypto.crypt(EncryptedText)
>>> print DecryptedText
Hello, World!
Bookmarks