Please read
PHP: Methods to encrypt and decrypt with private key (part 1)
Method 2: use arcfour algorithm
function _tutorialspots_2($str, $key) { $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; }
Example usage:
echo _tutorialspots_2('your password', 'your private key');
result:
ï·Ü¹;vr%¿g
It isn’t a nice result, we need to convert to a nice result:
- base64_encode
echo base64_encode( _tutorialspots_2('your password', 'your private key') );
Result:
77cOFdy5O3YaciW/Zw==
- unpack
$unpack = unpack('H*',_tutorialspots_2('your password', 'your private key')); echo $unpack[1];
Result:
efb70e15dcb93b761a7225bf67
To decrypt, we use this code:
echo _tutorialspots_2('ï·Ü¹;vr%¿g','your private key');
We get the result:
your password
If we encrypt the encrypting string with base64_encode function, we use this code:
echo _tutorialspots_2(base64_decode('77cOFdy5O3YaciW/Zw=='),'your private key');
If we encrypt the encrypting string with unpack function, we use this code:
echo _tutorialspots_2(pack('H*','efb70e15dcb93b761a7225bf67'),'your private key');
Next: PHP: Methods to encrypt and decrypt with private key (part 3)