php加密算法的应用,微信小程序 加密数据解密算法 使用 PHP 的 openssl_decrypt

解密我记得要微信文档里:微信加密说明

这里用Thinkphp3.2例子

$iv = define_str_replace(I('get.iv')); //把空格转成+

$encryptedData = urldecode(I('get.encryptedData')); //解码

$code = define_str_replace(I('get.code')); //把空格转成+

$msg = $this->getUserInfo($code,$encryptedData,$iv); //获取微信用户信息(openid)

public function getUserInfo($code,$encryptedData,$iv)

{

$appid = '你的appid';

$secret = '你的密钥';

$grant_type='authorization_code';

$url='https://api.weixin.qq.com/sns/jscode2session';

$url= sprintf("%s?appid=%s&secret=%s&js_code=%s&grant_type=%",$url,$appid,$secret,$code,$grant_type);

$user_data=json_decode(file_get_contents($url));

$session_key= define_str_replace($user_data->session_key);

$data="";

vendor('Vendor.WxApp.wxBizDataCrypt',realpath('./'),'.php');//引入微信解密文件

$wxBizDataCrypt = new \WXBizDataCrypt($appid,$session_key);

$errCode=$wxBizDataCrypt->decryptData($encryptedData,$iv,$data);

return ['errCode'=>$errCode,'data'=>json_decode($data),'session_key'=>$session_key];

}

/**

* 请求过程中因为编码原因+号变成了空格

* 需要用下面的方法转换回来

*/

function define_str_replace($data)

{

return str_replace(' ','+',$data);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值