If a=b ^ c; then b=a ^ c (^ means XOR). When processing XOR characters, PHP first converts the characters into binary ASCII values, and performs XOR on these values. After obtaining the result, convert the ASCII value into characters. If it is too late, please paste the implementation code directly:
Copy the code The code is as follows:
echo '';
$str='Hello world';
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 $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 $tmp.=substr($str,$i,1) ^ substr($key,$ i,1);
}
return $tmp;
}
$key='cc';
$jh=jiami($str, $key);
echo ' Before encryption: '.$str.'
';
echo 'After encryption: '.$jh.'
';
echo 'After decryption:'.jiemi($jh, $key).'
';
With the opportunity to improve, this function implements simple encryption and decryption
http://www.bkjia.com/PHPjc/313657.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313657.htmlTechArticleIf a=b ^ c; then b=a ^ c (^ means XOR), php in When processing XOR characters, first convert the characters into binary ASCII values, perform XOR on these values, and then convert the ASCII values after obtaining the results...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn