<?php
/*****************************加密*******************************/
$key = "miyao";//密钥
$string="jiami";//需要加密的字符
//自带的加密函数
$crypttext = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
$encrypted =trim(safe_b64encode($crypttext));//对特殊字符进行处理
echo $encrypted;
//执行结果:c1JEeW8xWGkwMWVoaTRrWm4zdHVEYnI0WkltSlZmSG5ZN2EwNENBYnprZz0=
/*****************************加密*******************************/
/*****************************解密*******************************/
$key="miyao";//密钥
$encrypted = ‘c1JEeW8xWGkwMWVoaTRrWm4zdHVEYnI0WkltSlZmSG5ZN2EwNENBYnprZz0=‘;//需要解密的字符
$crypttexttb = safe_b64decode($encrypted);//对特殊字符解析
$decryptedtb = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($crypttexttb), MCRYPT_MODE_CBC, md5(md5($key))), "\0");//解密函数
echo $decryptedtb;
//执行结果:jiami
/*****************************解密*******************************/
//处理特殊字符
function safe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array(‘+‘,‘/‘,‘=‘),array(‘-‘,‘_‘,‘‘),$data);
return $data;
}
//解析特殊字符
function safe_b64decode($string) {
$data = str_replace(array(‘-‘,‘_‘),array(‘+‘,‘/‘),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr(‘====‘, $mod4);
}
return base64_decode($data);
}
?>