Created
October 5, 2013 09:20
-
-
Save intech/6838705 to your computer and use it in GitHub Desktop.
rc4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Encrypt given plain text using the key with RC4 algorithm. | |
* All parameters and return value are in binary format. | |
* | |
* @param string key - secret key for encryption | |
* @param string pt - plain text to be encrypted | |
* @return string | |
*/ | |
exports = module.exports = function rc4(key, pt) { | |
s = new Array(); | |
for (var i = 0; i < 256; i++) { | |
s[i] = i; | |
} | |
var j = 0; | |
var x; | |
for (i = 0; i < 256; i++) { | |
j = (j + s[i] + key.charCodeAt(i % key.length)) % 256; | |
x = s[i]; | |
s[i] = s[j]; | |
s[j] = x; | |
} | |
i = 0; | |
j = 0; | |
var ct = ''; | |
for (var y = 0; y < pt.length; y++) { | |
i = (i + 1) % 256; | |
j = (j + s[i]) % 256; | |
x = s[i]; | |
s[i] = s[j]; | |
s[j] = x; | |
ct += String.fromCharCode(pt.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]); | |
} | |
return ct; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rc4 = require('./rc4'); | |
// RC4 PRIVATE KEY | |
var key = '123456789'; | |
var file = 'test123456'; | |
// Encrypt | |
var encfile = rc4(key, file); | |
console.log(encfile); | |
// Decrypt | |
var decfile = rc4(key, encfile); | |
console.log(decfile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment