RC4 Encryption
May 18th, 2005 by Intermanaut
To use this code, simply call the EnDeCrypt function with two parameters: the string you want to encrypt/decrypt; the encryption password.
<?
function EnDeCrypt($plaintext,$strPwd)
{
$intLength=strlen($strPwd);
$sbox=Array();
$key=Array();
$cipher=”;
$i=0;
$j=0;
for ($a=0; $a<256; $a++)
{
$key[$a]=ord(substr($strPwd,($a % $intLength),1));
$sbox[$a]=$a;
}
$b=0;
for ($a=0; $a<256; $a++)
{
$b=($b + $sbox[$a] + $key[$a]) % 256;
$tempSwap = $sbox[$a];
$sbox[$a] = $sbox[$b];
$sbox[$b] = $tempSwap;
}
for ($a=0; $a<strlen($plaintext); $a++)
{
$i=($i+1) % 256;
$j=($j + $sbox[$i]) % 256;
$temp=$sbox[$i];
$sbox[$i]=$sbox[$j];
$sbox[$j]=$temp;
$k=$sbox[($sbox[$i] + $sbox[$j]) % 256 ];
$cipherby=ord(substr($plaintext,$a,1)) ^ $k;
$cipher=$cipher . chr($cipherby);
}
return $cipher;
}
$strTemp=EnDeCrypt(“String to encrypt”, “Cipher password”);
?>