Skip to content

Instantly share code, notes, and snippets.

@rndme
Created May 11, 2016 12:42
Show Gist options
  • Save rndme/d25cecb8c6b41c33f98dea01b73d21f9 to your computer and use it in GitHub Desktop.
Save rndme/d25cecb8c6b41c33f98dea01b73d21f9 to your computer and use it in GitHub Desktop.
// public domain, from wikipedia description: https://en.wikipedia.org/wiki/RC4#Spritz
function rc4(key, str) {
var s = [],
j = 0,
x, res = [],
i, j, y, l = key.length,
m = str.length;
for(i = 0; i < 256; i++) s[i] = i;
for(i = 0; i < 256; i++) {
j = (j + s[i] + key.charCodeAt(i % l)) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
}
for(y = 0, i = 0, j = 0; y < m; y++) {
j = (j + s[i = (i + 1) % 256]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
res.push(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
}
return String.fromCharCode.apply(0, res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment