zoukankan      html  css  js  c++  java
  • php 解密小程序获取unionid

    小程序代码



    php代码

    public function login2()
    {
    $post = input();
    if (!empty($post)) {
    $appid = $this->wxappid;
    $secret = $this->wxsecret;
    if(isset($post['code'])) $code = $post['code'];
    if(isset($post['iv'])) $iv = $post['iv'];
    if(isset($post['rawData'])) $rawData = $post['rawData'];
    if(isset($post['signature'])) $signature = $post['signature'];
    if(isset($post['encryteData'])) $encryptedData = $post['encryteData'];
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
    $weixin = file_get_contents($url);
    $jsondecode = json_decode($weixin);
    $res = get_object_vars($jsondecode);
    $sessionKey = $res['session_key'];//取出json里对应的值
            // 验证签名
    $signature2 = sha1(htmlspecialchars_decode($rawData) . $sessionKey);
    if ($signature2 !== $signature) return json("signNotMatch");
                $data = [];
                $errCode = $this->decryptData($encryptedData, $iv, $sessionKey, $data);

    if ($errCode == 0) {
    return $data;
    } else {
    return json('获取失败');
    }
    }
    }
    public function decryptData( $encryptedData, $iv,$sessionKey, &$data )
    {
    if (strlen($sessionKey) != 24) {
    return json('sessionKey错误');
    }
    $aesKey=base64_decode($sessionKey);


    if (strlen($iv) != 24) {
    return json('iv错误');
    }
    $aesIV=base64_decode($iv);
    $aesCipher=base64_decode($encryptedData);
    $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
    $dataObj=json_decode( $result );
    if( $dataObj == NULL )
    {
    return json('IllegalBuffer错误');
    }
    if( $dataObj->watermark->appid != $this->wxappid )
    {
    return json('IllegalBuffer错误');
    }
    $data = $result;
    return $data;
    }
  • 相关阅读:
    jsonp跨站请求
    ModelForm验证实例
    ModelForm验证笔记
    Form验证实例
    Form验证笔记
    模板导入_分页_cookie_装饰器_实例
    k8s的ingress资源简述
    k8s资源配置清单的书写格式(yaml文件)
    k8s的service简述
    k8s的Pod控制器
  • 原文地址:https://www.cnblogs.com/sk-wlf/p/9913737.html
Copyright © 2011-2022 走看看