The basis of this algorithm: the character A B,a^b=c,c^b=a, that is, two times the XOR operation can get the original character. The implementation code is as follows:
/** * @desc encryption * @param string $STR string to be encrypted * @param string $key key * @return string*/functionEncrypt$str,$key){ $mixStr=MD5(Date(' y-m-d h:i:s ').Rand(1000)); $tmp= ' '; $strLen=strlen($str); for($i=0,$j= 0;$i<$strLen;$i++,$j++){ $j=$j= = 32? 0:$j; $tmp.=$mixStr[$j]. ($str[$i] ^$mixStr[$j]); } return Base64_encode(Bind_key ($tmp,$key));}/** * @desc Decrypt * @param string $str to be decrypted * @param string $key key * @return string*/functionDecrypt$str,$key){ $str= Bind_key (Base64_decode($str),$key); $strLen=strlen($str); $tmp= ' '; for($i= 0;$i<$strLen;$i++){ $tmp.=$str[$i] ^$str[++$i]; } return $tmp;}/** * @desc helper methods use the key to manipulate the string after randomization * @param $str * @param $key * @return string*/functionBind_key ($str,$key){ $encrypt _key=MD5($key); $tmp= ' '; $strLen=strlen($str); for($i=0,$j= 0;$i<$strLen;$i++,$j++){ $j=$j= = 32? 2 x$j; $tmp.=$str[$i] ^$encrypt _key[$j]; } return $tmp;}
PHP simple encryption and decryption method