Skip to content

Instantly share code, notes, and snippets.

@kennwhite
Forked from dchest/rc4.js
Last active August 29, 2015 14:08
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 kennwhite/6ce8a51731265a4021d6 to your computer and use it in GitHub Desktop.
Save kennwhite/6ce8a51731265a4021d6 to your computer and use it in GitHub Desktop.
function rc4(f,F,g,G){for(var U=256,b=[],c=a=0,d;U>a;a++)b[a]=a;for(a=0;U>a;a++)c=(c+b[a]+f[a%F])%U,d=b[a],b[a]=b[c],b[c]=d;for(var e=c=a=0;e<G;e++)a++,a%=U,c+=b[a],c%=U,d=b[a],b[a]=b[c],b[c]=d,g[e]=b[(b[a]+b[c])%U]}
// Usage:
//
var key = [75, 101, 121]; // input bytes: "Key"
var out = new Array(10); // place for keystream bytes
rc4(key, key.length, out, out.length); // out now contains keystream: [235, 159, 119, 129, 183, 52, 202, 114, 167, 25]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment