芝麻信用SDK公钥问题

芝麻信用的文档真实乱的一B。

芝麻信用下发给你的公钥是一行的格式,在phpsdk中不能直接使用(openssl系列函数不认),需要转换成每行64个字符的那种格式,也就是命令行生成的文件那种格式,比如:

-----BEGIN PUBLIC KEY-----
KBgQDLSwfRSwJcbfMA0GCSqGSIb3DQEBAzrSxc2Fl1MIGQUAA4GNADCBiQ0/LJuW
FTN9SLDc6VA0Bye6HbZdVpPP7qxYiv1hpfyCkAQWo2JpTrgFmSOBYyLwhxKyHpC5
o/YwtBl3vbH9H3RHJiu8BBNwFxXkqg8lQWPTCsYG2zCRoG0Woo4CgsWNC0DBPPmV
l4b5pDLG1O8AAncscQIDAQAB
-----END PUBLIC KEY-----

直接上代码:

<?php
    include('./ZmopClient.php');
    include('./ZhimaAuthInfoAuthorizeRequest.php');
    class TestAuthFreeze {
        //芝麻信用网关地址
        public $gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";
        //商户公钥文件
        //芝麻公钥文件
        public $privateKeyFile = "path/rsa_private_key.pem";
        public $zmPublicKeyFile = "path/zima_public_key.pem";

        //数据编码格式
        public $charset = "UTF-8";
        //芝麻分配给商户的appId
        public $appId = "1000003";


    //生成移动端SDK 集成需要的sign 参数 ,并进行urlEncode
    public function generateSign($certNo,$name,$certType='IDENTITY_CARD'){
        $client = new ZmopClient($this->gatewayUrl, $this->appId, $this->charset, $this->privateKeyFile,$this->zmPublicKeyFile);
        $request = new ZhimaAuthInfoAuthorizeRequest();
        $request->setScene("test");
        // 授权来源渠道设置为appsdk
        $request->setChannel("appsdk");
        // 授权类型设置为2标识为证件号授权见“章节4中的业务入参说明identity_type”
        $request->setIdentityType("2");
        // 构造授权业务入参证件号,姓名,证件类型;“章节4中的业务入参说明identity_param”
        $request->setIdentityParam("{\"certNo\":\"$certNo\",\"certType\":\"IDENTITY_CARD\", \"name\":\"$name\"}");
        // 构造业务入参扩展参数“章节4中的业务入参说明biz_params”
        $request->setBizParams("{\"auth_code\":\"M_APPSDK\"}");
       
        $params = $client->generateEncryptedParamWithUrlEncode($request);
        $sign = $client->generateSignWithUrlEncode($request);

        $data['gatewayUrl'] = $this->gatewayUrl;
        $data['appId'] = $this->appId;
        $data['charset'] = $this->charset;
        $data['params']=$params;
        $data['sign'] = $sign;
        return $data;
    }




    // 解密
    public function zhimacallback($params){
        $this->privateKeyFile= "path/rsa_private_key.pem";
        $client = new ZmopClient($this->gatewayUrl, $this->appId, $this->charset, $this->privateKeyFile,$this->zmPublicKeyFile);
        $result=$client->generateSignCallBack($params,$this->privateKeyFile);
        return $result;
    }
} 
?>