类代码:
key; } srand((double)microtime()*1000000); $encrypt_key = md5(rand(0,32000)); $ctr=0; $tmp = ''; for ($i=0;$ikeyED($tmp,$key); } public function decrypt($txt,$key=''){ if(empty($key)){ $key=$this->key; } $txt = $this->keyED($txt,$key); $tmp = ''; for ($i=0;$i key=$key; } public function getPK(){ return $this->key; } }
使用方法:
setKey('qttc');
// 加密字符串
$enc = $jiami->encrypt($string,$jiami->getPK());
// 解密字符串
$dec = $jiami->decrypt($enc,$jiami->getPK());
echo '';
echo '加密前 : '.$string .'
';
echo '加密后 : '.$enc .'
';
echo '解密后 : '.$dec;
?>页面执行结果
结果1:

结果2:

由以上结果可以看到,每次加密产生的加密字符串都不一样,这是随机的。
解密的时候,需要使用加密时的公钥,否则无法解密。如你这边加密公钥使用‘qttc',解密的时候,也需要使用这个'qttc'作为公钥去解密,否则无法解密。