不多说 直接上代码
/**
* des加密
* @param [type] $str [description]
* @param [type] $key [description]
* @param boolean $toBase64 [description]
* @return [type] [description]
*/
function encryptStr($str, $key, $toBase64 = true)
{
$block = mcrypt_get_block_size('des', 'ecb');
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
$enc_str = mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
return (true == $toBase64) ? base64_encode($enc_str) : $enc_str;
}
/**des解密
* @param $str
* @param $key
* @return mixed
*/
function decryptStr($str, $key){
$str = base64_decode($str);
$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
$block = mcrypt_get_block_size('des', 'ecb');
$pad = ord($str[($len = strlen($str)) - 1]);
$res = urldecode(substr($str, 0, strlen($str) - $pad));
return json_decode(urldecode($res),true);
}
以上就是des+base64加密解密 用法如下
$key = '123456';
$data = '4564564654dfsfdsfsdfsfsffafas';
//加密
$res = urlencode(encryptStr(urlencode(json_encode($data,JSON_UNESCAPED_UNICODE)), $key))
var_dump($res);
//解密
$res1 = decryptStr(urldecode($data),$key);
var_dump($res1);