PHP实现对微信支付v3版本回调数据的解密

PS:本文使用了微信官方给出的demo来实现对回调数据的解密,本文主要对微信官方给出的demo如何使用作出部分个人讲解,以及对解密前后数据的格式进行展示

PHP类:这是微信官方给出的demo

<?php

class AesUtil
{
   
    /**
     * AES key
     *
     * @var string
     */
    public $aesKey = '此处填写你的APIv3密钥';

    const KEY_LENGTH_BYTE = 32;
    const AUTH_TAG_LENGTH_BYTE = 16;

    /**
     * Constructor
     */
    public
    function __construct()
    {
   
        $aesKey = '此处填写你的APIv3密钥';
        if (strlen($aesKey) != self::KEY_LENGTH_BYTE) {
   
            throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
        }
        $this->aesKey = $aesKey;
    }

    /**
     * Decrypt AEAD_AES_256_GCM ciphertext
     *
     * @param string $associatedData AES GCM additional authentication data
     * @param string $nonceStr AES GCM nonce
     * @param string $ciphertext AES GCM cipher text
     *
     * @return string|bool      Decrypted string on success or FALSE on failure
     */
    public
    function decryptToString($associatedData, $nonceStr, $ciphertext)
    {
   
        $ciphertext = \base64_decode($ciphertext);
        if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信支付v3版本回调验签和解密操作分别需要使用APIv3密钥和APIv3证书进行操作。下面是Java代码示例: 验签操作: ```java import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.util.SignUtils; public class WechatPayV3SignUtil { /** * 验证签名 * * @param serialNo 商户API证书序列号 * @param timestamp 时间戳 * @param nonce 随机串 * @param body 回调体 * @param signature 微信支付签名 * @param platformCert 平台证书内容 * @return true验证通过,false验证失败 * @throws TencentCloudSDKException 抛出异常 */ public static boolean verify(String serialNo, String timestamp, String nonce, String body, String signature, String platformCert) throws TencentCloudSDKException { String message = timestamp + "\n" + nonce + "\n" + body + "\n"; return SignUtils.verify(signature, message.getBytes(), platformCert, serialNo); } } ``` 解密操作: ```java import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.util.AesUtils; public class WechatPayV3DecryptUtil { /** * 解密回调通知 * * @param associatedData 附加数据 * @param nonce 随机串 * @param ciphertext 密文 * @param apiV3Key APIv3密钥 * @return 解密后的明文 * @throws TencentCloudSDKException 抛出异常 */ public static String decrypt(String associatedData, String nonce, String ciphertext, String apiV3Key) throws TencentCloudSDKException { byte[] keyBytes = apiV3Key.getBytes(); byte[] associatedDataBytes = associatedData.getBytes(); byte[] nonceBytes = nonce.getBytes(); byte[] ciphertextBytes = AesUtils.base64Decode(ciphertext); byte[] plainBytes = AesUtils.decryptToString(keyBytes, associatedDataBytes, nonceBytes, ciphertextBytes).getBytes(); return new String(plainBytes); } } ``` 以上代码仅供参考,具体实现需要根据自己的业务情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值