1、非对称加密
/**
*加密与解密函数
**/
function mymd5($string,$action="EN",$rand=''){ //字符串加密和解密
if($action=="DE"){//处理+号在URL传递过程中会异常
$string = str_replace('SLB','+',$string);
}
$secret_string = $rand.'4*j,.^&;?.%#@!'; //绝密字符串,可以任意设定
if(!is_string($string)){
$string=strval($string);
}
if($string==="") return "";
if($action=="EN") $md5code=substr(md5($string),8,10);
else{
$md5code=substr($string,-10);
$string=substr($string,0,strlen($string)-10);
}
//$key = md5($md5code.$_SERVER["HTTP_USER_AGENT"].$secret_string);
$key = md5($md5code.$secret_string);
$string = ($action=="EN"?$string:base64_decode($string));
$len = strlen($key);
$code = "";
for($i=0; $i<strlen($string); $i++){
$k = $i%$len;
$code .= $string[$i]^$key[$k];
}
$code = ($action == "DE" ? (substr(md5($code),8,10)==$md5code?$code:NULL) : base64_encode($code)."$md5code");
if($action=="EN"){//处理+号在URL传递过程中会异常
$code = str_replace('+','SLB',$code);
}
return $code;
}
mymd5('0s2Z1ovs0qOIbcad6e616b','DE');//解密
mymd5('test');//加密
2、对称加密和解密
function decrypt($data){
$privateKey = 'WZQGYL2016WZQGYL';
$iv='0000000011111111';
$encryptedData = base64_decode($data);
return $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
}
$privateKey = 'WZQGYL2016WZQGYL';
$iv='0000000011111111';
$encryptedData = base64_decode($data);
return $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
}