加密步骤:
1.获取13位时间戳
2.对时间戳进行md5加密,获取32位小写key
3.对key进行加工,key=li+key
4.将key转换为byte数组,获取前8个字符作为key2
5.用key2对原body进行des加密,加密模式ECB,填充pkcs7padding,输出hex,字符集utf8body
/**
* 加密
* @param $data
* @return mixed
*/
public function encrypt($data)
{
$timestamp = $this->msectime();//当前时间戳13位
$key = 'li'.md5($timestamp);
$key_arr = $this->getbytes($key);
$key2 =array_slice($key_arr,0,8);
$en = openssl_encrypt($data, 'DES−ECB',$key2,OPENSSL_RAW_DATA); //des加密,模式ecb,OPENSSL_RAW_DATA方式【会用PKCS#7进行补位】
$en = bin2hex($en); //输出hex
return $en;
}
/**
* 转换一个string字符串为byte数组
* @param $str 需要转换的字符串
* @param $bytes 目标byte数组
*/
protected function getbytes($str) {
$len = strlen($str);
$bytes = array();
for($i=0;$i<$len;$i++) {
if(ord($str[$i]) >= 128){
$byte = ord($str[$i]) - 256;
}else{
$byte = ord($str[$i]);
}
$bytes[] = $byte ;
}
return $bytes;
}
/**
* 生成13位时间戳
* @return int
*/
protected function msectime() {
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return intval($msectime);
}
查看当前PHP版本支持的加密方式
return openssl_get_cipher_methods();
die;