通过微信开源封装weixin-java-miniapp解密手机号出现:me.chanjar.weixin.common.error.WxRuntimeException: AES解密失败!

通过微信开源封装weixin-java-miniapp解密手机号出现:me.chanjar.weixin.common.error.WxRuntimeException: AES解密失败! 特此记录

1. 环境 :jdk1.8
2. 依赖
        
        
            com.github.binarywang
            weixin-java-miniapp
            4.1.0
        
3. controller层接口实现
    @ApiOperation(value = "小程序用户登录接口")
    @PostMapping(value = "/login")
    public Result login(@ApiIgnore @RequestParam Map parameters) {
        return wxService.login(parameters);
    }
4. 小程序登录实现
       String code = parameters.get("code");
       String encryptedData = parameters.get("encryptedData");
       String iv = parameters.get("iv");
     // 通过code获取sessionInfo
       WxMaJscode2SessionResult sessionInfo = null;
        try {
            sessionInfo = wxMaService.getUserService().getSessionInfo(code);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

//获取sessionKey
String sessionKey = sessionInfo.getSessionKey();
...........
// 手机号查询
 WxMaPhoneNumberInfo phoneNumberInfo=wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
............
/ 解密 encryptedData 获取用户信息
 WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
5.异常问题:
me.chanjar.weixin.common.error.WxRuntimeException: AES解密失败!
6. 发现问题:
  • 前端传参:http://ip:端口/api.app/v1/wx/login?code=073fOLll2c21y94kgDll2wdHjB2fOLlF&iv=ufJc0dtGYaLvKw/lwQvE8g==&encryptedData=7Kt4S3skEyeNXvmeQgoXtofCGmNH6gzTlzt3tGbn/g2NsTzd7T8mvUI6qElxY21tt9WI6p3ju5XeOYdG46tSHL4tqHDv2Q05VVanDz7n4wO03zhydqLI8Ts+tX/UsOFMOIOP0MJKhetFs5DfsF0SG6cCjoC98UOwZcwySS2wqhfQNbdRbhCq0fQU3w21vDAbxulX2tZvTEo7yNKt+Dd30A==
  • 分解参数:
code=073fOLll2c21y94kgDll2wdHjB2fOLlF

iv=ufJc0dtGYaLvKw/lwQvE8g==

encryptedData=7Kt4S3skEyeNXvmeQgoXtofCGmNH6gzTlzt3tGbn/g2NsTzd7T8mvUI6qElxY21tt9WI6p3ju5XeOYdG46tSHL4tqHDv2Q05VVanDz7n4wO03zhydqLI8Ts+tX/UsOFMOIOP0MJKhetFs5DfsF0SG6cCjoC98UOwZcwySS2wqhfQNbdRbhCq0fQU3w21vDAbxulX2tZvTEo7yNKt+Dd30A==
  • 后端获取参数
code= 073fOLll2c21y94kgDll2wdHjB2fOLlF

iv=ufJc0dtGYaLvKw/lwQvE8g==

encryptedData=7Kt4S3skEyeNXvmeQgoXtofCGmNH6gzTlzt3tGbn/g2NsTzd7T8mvUI6qElxY21tt9WI6p3ju5XeOYdG46tSHL4tqHDv2Q05VVanDz7n4wO03zhydqLI8Ts tX/UsOFMOIOP0MJKhetFs5DfsF0SG6cCjoC98UOwZcwySS2wqhfQNbdRbhCq0fQU3w21vDAbxulX2tZvTEo7yNKt Dd30A==


  • 参数对比得到:后端获取encryptedData参数中“+”(加号) 转换为空格,导致解密失败
问题解决:

密文串要用 json 格式传输,不能用参数拼接的方式,会把密文串的加号转换为空格,导致解密失败。

通过微信开源封装weixin-java-miniapp解密手机号出现:me.chanjar.weixin.common.error.WxRuntimeException: AES解密失败!_第1张图片

7.结束

解密成功
通过微信开源封装weixin-java-miniapp解密手机号出现:me.chanjar.weixin.common.error.WxRuntimeException: AES解密失败!_第2张图片

你可能感兴趣的:(异常,java,微信,java)