<?php
const v3key = '*********';
const pr = '******';
const mchid = '*******';
const serial_no = '*********';
$url = 'https://api.mch.weixin.qq.com/v3/certificates';
$url_parts = parse_url($url);
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$stime =time();
$nostr = md5(time().mt_rand(000,999));
$message = "GET"."\n".
$canonical_url."\n".
$stime ."\n".
$nostr ."\n"."\n";
openssl_sign($message, $sign, file_get_contents(pr), 'sha256WithRSAEncryption');
$sign = base64_encode($sign);
$token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
mchid, str_replace("\n","",$nostr), str_replace("\n","",$stime), serial_no, $sign);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Authorization: WECHATPAY2-SHA256-RSA2048 ' .$token,
'Accept:application/json',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'));
$crt = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($crt, 0, $headerSize);
$body = substr($crt, $headerSize);}
curl_close($ch);
$str = get_header($header);
$crs = json_decode($body,true);
$m = $crs['data'][0]['encrypt_certificate']['ciphertext'];
$m = base64_decode($m);
$add_data = $crs['data'][0]['encrypt_certificate']['associated_data'];
$nonce = $crs['data'][0]['encrypt_certificate']['nonce'];
$crt = sodium_crypto_aead_aes256gcm_decrypt($m,$add_data,$nonce,v3key);
$res = openssl_pkey_get_public($crt);
$ttr= openssl_pkey_get_details($res);
$ttr = $ttr['key'];
$v_sj =$str['Wechatpay-Timestamp']."\n".
$str['Wechatpay-Nonce']."\n".
$body."\n";
$wx_k = openssl_verify($v_sj,base64_decode($str['Wechatpay-Signature']),$ttr,'sha256WithRSAEncryption');
if($wx_k === 1){
echo '数据获取成功,你的微信支付平台证书序列号为:'.$str['Wechatpay-Serial']."<br>"."CR证书为:"."<br>".$crt."<br>"."公共秘钥为:"."<br>".$ttr."<br>"."已为你保存到文件,青岛系统里面进行查看。";
file_put_contents('wx.crt',$crt);
file_put_contents('wx.pub',$ttr);
}else{
echo '验签不通过,所有数据不予展示,请联系管理员';
}
验签名使用的是openssl 扩展,解密使用的是另一个扩展,关联是你的业务逻辑是要验证签名还是解密数据。
你好,我生成签名是成功,也拿到了平台证书列表,并且证书列表的序列号和微信返回的header里面的也是一致的(满足验签前提条件之一),组装的验签串也是符合要求,并且也解密了响应体,拿到了公钥,签名也拿到了,但是进行验签时,openssl_verify()返回0.
是微信给你发送的通知?你进行验签,一直不成功?
i你好,请问文中的$str = get_header($header);//把header信息解析成数组 get_header()是不是一个自定义函数?
你好,这是一个自定义函数。