if a=b ^ C; then B=a ^ C (^ is the meaning of XOR), PHP in the processing of different or character characters first convert the characters into binary ASCII values, or, to obtain the results after the ASCII value into the character, the principle of a direct paste implementation of the code:
Copy Code code as follows:
Echo ' <meta charset= ' utf-8 ' > ';
$str = ' World, hello ';
function Jiami ($STR, $key) {
$key =md5 ($key);
$k =md5 (rand (0,100));//equivalent to dynamic key
$k =substr ($k, 0, 3);
$tmp = "";
for ($i =0; $i <strlen ($STR); $i + +) {
$tmp. =substr ($str, $i, 1) ^ substr ($key, $i, 1);
}
Return Base64_encode ($k. $tmp);
}
function Jiemi ($STR, $key) {
$len =strlen ($STR);
$key =md5 ($key);
$str =base64_decode ($STR);
$str =substr ($STR, 3, $len-3);
$tmp = "";
for ($i =0; $i <strlen ($STR); $i + +) {
$tmp. =substr ($str, $i, 1) ^ substr ($key, $i, 1);
}
return $tmp;
}
$key = ' CC ';
$jh =jiami ($str, $key);
Echo ' Before encryption: '. $str. ' <br> ';
Echo ' encrypted: '. $jh. ' <br> ';
Echo ' after decryption: '. Jiemi ($jh, $key). ' <br> ';
There is a chance in the perfect, this function is to achieve a simple encryption and decryption