private $iv = ‘lua123456789qwer‘;#随便填写16个数private $key = ‘123456789asdfghjkl$$$$$‘;#随便写多少
//加密
function encryptlua($str) {
//$key = $this->hex2bin($key);
$iv = $this - >iv;
$td = mcrypt_module_open(‘rijndael - 128‘, ‘‘, ‘cbc‘, $iv);
mcrypt_generic_init($td, $this - >key, $iv);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return bin2hex($encrypted);
}
//解密
function decryptlua($str) {
//$key = $this->hex2bin($key);
$str = $this - >hex2bin($str);
$iv = $this - >iv;
$td = mcrypt_module_open(‘rijndael - 128‘, ‘‘, ‘cbc‘, $iv);
mcrypt_generic_init($td, $this - >key, $iv);
$decrypted = mdecrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return utf8_encode(trim($decrypted));
}
protected
function hex2bin($hexdata) {
$bindata = ‘‘;
for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata. = chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
来源: http://www.bubuko.com/infodetail-2051343.html