Skip to content

Instantly share code, notes, and snippets.

@meejah meejah/
Created Oct 22, 2014

What would you like to do?
Attempt at a Twitter-sized RC4 implementation
# attempt to make a tweet-sized RC4
# this is 241 characters... :/
# return value comes in "z" which must be an empty array to start
def R(k,C,z):
def s(A,x,y):A[x],A[y]=A[y],A[x]
for i in r(x):j=(j+S[i]+o(k[i%l(k)]))%x;s(S,i,j)
while l(C):i=(i+1)%x;j=(j+S[i])%x;s(S,i,j);K=S[(S[i]+S[j])%x];z.append(chr((o(C[0])^K)))\
# let's test it!
# couple cheezy tests from Wikipedia
for key, plain, cypher in [
('Key', 'Plaintext', 'BBF316E8D940AF0AD3'.decode('hex_codec')),
('Wiki', 'pedia', '1021BF0420'.decode('hex_codec'))]:
decrypt = []
R(key, cypher, decrypt)
if ''.join(decrypt) == plain:
print "success", key, cypher.encode('hex_codec'), '=', plain
print "error", key, cypher, '!=', plain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.