Skip to content

Instantly share code, notes, and snippets.

@RobinDavid
Created February 24, 2014 21:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RobinDavid/9197294 to your computer and use it in GitHub Desktop.
Save RobinDavid/9197294 to your computer and use it in GitHub Desktop.
RC4 algorithm implementation
def RC4(data, key):
x = 0
s = range(256)
for i in range(256):
x = (x + s[i] + ord(key[i % len(key)])) % 256
s[i], s[x] = s[x], s[i]
x = y = 0
out = ""
for c in data:
x = (x + 1) % 256
y = (y + s[x]) % 256
s[x], s[y] = s[y], s[x]
out += chr(ord(c) ^ s[(s[x] + s[y]) % 256])
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment