Skip to content

Instantly share code, notes, and snippets.

@meejah
Created October 22, 2014 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meejah/629b85fea6744981efdc to your computer and use it in GitHub Desktop.
Save meejah/629b85fea6744981efdc to your computer and use it in GitHub Desktop.
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