123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- /**
- * 手机短信接口
- */
- class Test extends Api
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- //验签,2048位,265截取
- public function verifysign(){
- //解密签名开始
- $sign = $this->request->request('sign','','trim');
- if(empty($sign)){
- $this->error('缺少签名');
- }
- $sign = base64_decode($sign);
- $private_key_str = config('app_rsa.private_key');
- $private_key = "-----BEGIN RSA PRIVATE KEY-----" .PHP_EOL.
- wordwrap($private_key_str, 64, PHP_EOL, true) .
- PHP_EOL."-----END RSA PRIVATE KEY-----";
- $signgetdata = []; //被解密出来的数据
- $split_len = 256;
- $sign_split = str_split($sign, $split_len);
- foreach($sign_split as $key => $sign_val){
- $signgetdata_child = null;
- openssl_private_decrypt($sign_val, $signgetdata_child, $private_key); // 使用私钥解密数据
- $signgetdata[] = $signgetdata_child;
- }
- $signgetdata = implode('',$signgetdata);
- if (!$signgetdata) {
- $this->error('签名错误1');
- }
- dump($signgetdata);
- //解密签名结束
- //接收到的参数,组成我自己的验签体string
- $request_all = $this->request->request();
- unset($request_all['s']);
- unset($request_all['sign']);
- ksort($request_all);
- $request_str = '';
- foreach($request_all as $key => $param){
- $request_str .= $key.'='.$param.'&';
- }
- $request_str .= 'signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
- dump($request_str);
- //作对比
- if($request_str != $signgetdata){
- $this->error('验签错误');
- }
- echo '验签正确';
- }
- //验签
- public function verifysign_old(){
- //解密签名
- $sign = $this->request->request('sign','','trim');
- if(empty($sign)){
- $this->error('缺少签名');
- }
- $sign = base64_decode($sign);
- $private_key_str = config('app_rsa.private_key');
- $private_key = "-----BEGIN RSA PRIVATE KEY-----" .PHP_EOL.
- wordwrap($private_key_str, 64, PHP_EOL, true) .
- PHP_EOL."-----END RSA PRIVATE KEY-----";
- $signgetdata = ''; //被解密出来的数据
- openssl_private_decrypt($sign, $signgetdata, $private_key); // 使用私钥解密数据
- if (!$signgetdata) {
- $this->error('签名错误');
- }
- //dump($signgetdata);
- //接收到的参数
- $request_all = $this->request->request();
- unset($request_all['s']);
- unset($request_all['sign']);
- ksort($request_all);
- $request_all = http_build_query($request_all);
- $request_all .= '&signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
- //dump($request_all);
- //作对比
- if($request_all != $signgetdata){
- $this->error('验签错误');
- }
- //echo '验签正确';
- return true;
- }
- //加密
- public function jiami(){
- $request_all = $this->request->request();
- unset($request_all['s']);
- // dump($request_all);
- ksort($request_all);
- // dump($request_all);
- $request_all = http_build_query($request_all);
- $request_all .= '&signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
- // echo '加密字符串';
- // echo $request_all;
- $public_key_str = config('app_rsa.public_key');
- echo strlen($public_key_str);exit;
- $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
- wordwrap($public_key_str, 64, PHP_EOL, true) .
- PHP_EOL."-----END PUBLIC KEY-----";
- $sign = '';
- openssl_public_encrypt($request_all,$sign,$public_key);
- $sign = base64_encode($sign);
- echo '获得签名';
- dump($sign);
- }
- /**
- *
- * @return void
- */
- public function test1()
- {
- echo '<pre>';var_dump(1);exit;
- }
- }
|