appkey/secretKey 实例
最后更新于:2022-04-02 02:18:28
[TOC]
## 使用 appkey 与 secretKey 参数验证
新建 `demo.php` 执行 `php -S 127.0.0.1:8081`
```
/*======= 加密 start =========*/
$appkey ="w12baqefaaoc";
$secretKey = "asjdKHKJHasjdIAHSdkASkdbaksjbdkjas";
$url ="http://127.0.0.1:8081/demo.php?";
$params['appkey'] = $appkey;
$params['order_id']=1;
$params['name']="idcpj";
$params['pwd']="123456";
$params['time']=time();
$params['sign'] = getSign($params,$secretKey);
echo $url.http_build_query($params);
function getSign($query,$secretKey){
ksort($query);
$str = http_build_query($query);
return md5($str.$secretKey);
}
/*======= 加密 end =========*/
/*======= 解密 start =========*/
$conf['w12baqefaaoc']="asjdKHKJHasjdIAHSdkASkdbaksjbdkjas";
$getParams = $_GET;
if (abs(time() - $getParams['time'])>=3){//使用 abs 更加安全
die("
time out"); } if (valiSign($getParams,$conf)){ die("
succ"); }else{ die("
error"); } function valiSign($params,$conf){ $paramsSign = $params['sign']; unset($params['sign']); ksort($params); $str = http_build_query($params); $cSign = md5($str.$conf[$params['appkey']]); return $paramsSign==$cSign; } /*======= 解密 end =========*/ ```
';
time out"); } if (valiSign($getParams,$conf)){ die("
succ"); }else{ die("
error"); } function valiSign($params,$conf){ $paramsSign = $params['sign']; unset($params['sign']); ksort($params); $str = http_build_query($params); $cSign = md5($str.$conf[$params['appkey']]); return $paramsSign==$cSign; } /*======= 解密 end =========*/ ```