/* | |
* RC4 symmetric cipher encryption/decryption | |
* | |
* @license Public Domain | |
* @param string key - secret key for encryption/decryption | |
* @param string str - string to be encrypted/decrypted | |
* @return string | |
*/ | |
function rc4(key, str) { | |
var s = [], j = 0, x, res = ''; | |
for (var i = 0; i < 256; i++) { | |
s[i] = i; | |
} | |
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; | |
for (var y = 0; y < str.length; y++) { | |
i = (i + 1) % 256; | |
j = (j + s[i]) % 256; | |
x = s[i]; | |
s[i] = s[j]; | |
s[j] = x; | |
res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]); | |
} | |
return res; | |
} |
<?php | |
/* | |
* RC4 symmetric cipher encryption/decryption | |
* | |
* @license Public Domain | |
* @param string key - secret key for encryption/decryption | |
* @param string str - string to be encrypted/decrypted | |
* @return string | |
*/ | |
function rc4($key, $str) { | |
$s = array(); | |
for ($i = 0; $i < 256; $i++) { | |
$s[$i] = $i; | |
} | |
$j = 0; | |
for ($i = 0; $i < 256; $i++) { | |
$j = ($j + $s[$i] + ord($key[$i % strlen($key)])) % 256; | |
$x = $s[$i]; | |
$s[$i] = $s[$j]; | |
$s[$j] = $x; | |
} | |
$i = 0; | |
$j = 0; | |
$res = ''; | |
for ($y = 0; $y < strlen($str); $y++) { | |
$i = ($i + 1) % 256; | |
$j = ($j + $s[$i]) % 256; | |
$x = $s[$i]; | |
$s[$i] = $s[$j]; | |
$s[$j] = $x; | |
$res .= $str[$y] ^ chr($s[($s[$i] + $s[$j]) % 256]); | |
} | |
return $res; | |
} | |
?> |
This comment has been minimized.
This comment has been minimized.
Well, different implementations of the same algorithm couldn't be much different. |
This comment has been minimized.
This comment has been minimized.
Is it possible to get java compatible algorithm for the same? |
This comment has been minimized.
This comment has been minimized.
I'm not a Java developer but the code is simple and I think an average Java programmer can port it to Java. |
This comment has been minimized.
This comment has been minimized.
Javascript works, but PHP code doesn't work with unicode strings, like '€'. |
This comment has been minimized.
This comment has been minimized.
It looks like there is a difference in the last line of the PHP version, compared to JS: This is my PHP version, which works with unicode, at least on my server: function mb_chr($char) { function mb_ord($char) { if (is_array($result) === true) { function rc4($key, $str) { $s = array();
} |
This comment has been minimized.
This comment has been minimized.
Thats exactly what i need! PHP-Decryption and JS-Encryption. In my case I had to utf8_encode the decrypted string. Works fine, thanks! |
This comment has been minimized.
This comment has been minimized.
I need encryption and decryption for at least PHP. |
This comment has been minimized.
This comment has been minimized.
i need php encryption and js decryption for websocket messages |
This comment has been minimized.
This comment has been minimized.
But this is one-way. How about the snippet for decryption? |
This comment has been minimized.
This comment has been minimized.
Actually, @DamilolaJegede, this function is symmetrical. If you pass the resulting (encoded) string back into the function, you get the original string back. |
This comment has been minimized.
This comment has been minimized.
how to combine this rc4 with Message Authentication code? |
This comment has been minimized.
This comment has been minimized.
Hi Basgroot, I need to encript a series of _GET values before including them in the URL, and decript them in the target page in order to populate it, with the various _GET values. But the function does not decript the string as pairs of GET keys and values. How can I achieve this? |
This comment has been minimized.
This comment has been minimized.
Why is there no support for streaming? Or at least keeping the internal state? RC4 is a stream cipher so there has to be support for transforming data while keeping the internal state of the encryption/decryption SBox (https://en.wikipedia.org/wiki/RC4 see: Key scheduling) |
This comment has been minimized.
This comment has been minimized.
nevermind, this one does the trick |
This comment has been minimized.
This comment has been minimized.
Here's my port to AutoIt3:
|
This comment has been minimized.
just wonder if this, or its source ... was actually inspired by this one: http://code.google.com/p/sessionstorage/source/browse/trunk/src/RC4.js