Skip to content

Instantly share code, notes, and snippets.

@mckabi
Created December 16, 2013 16:54
Show Gist options
  • Save mckabi/7990284 to your computer and use it in GitHub Desktop.
Save mckabi/7990284 to your computer and use it in GitHub Desktop.
Rc4 python implement
def RC4crypt(data, key):
'''
http://en.wikipedia.org/wiki/RC4
'''
box_range = 256
box = range(box_range)
i = 0
for j in range(box_range):
i = (i + box[j] + ord(key[j % len(key)])) % box_range
box[j], box[i] = box[i], box[j]
i = j = 0
out = []
for c in data:
i = (i + 1) % box_range
j = (j + box[i]) % box_range
box[i], box[j] = box[j], box[i]
out.append(chr(ord(c) ^ box[(box[i] + box[j]) % 256]))
return ''.join(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment