Skip to content

Instantly share code, notes, and snippets.

@meejah meejah/rc4-min.py
Created Oct 22, 2014

Embed
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):
r=range;o=ord;x=256;l=len
def s(A,x,y):A[x],A[y]=A[y],A[x]
S=r(x);j=0
for i in r(x):j=(j+S[i]+o(k[i%l(k)]))%x;s(S,i,j)
i=j=0
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)))\
;C=C[1:]
# 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
else:
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.