Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid AES key length: 31 bytes 异常 #692

Closed
JohnsonYuYHao opened this issue Jul 27, 2018 · 2 comments
Closed

Invalid AES key length: 31 bytes 异常 #692

JohnsonYuYHao opened this issue Jul 27, 2018 · 2 comments
Labels

Comments

@JohnsonYuYHao
Copy link

JohnsonYuYHao commented Jul 27, 2018

异常:

com.github.binarywang.wxpay.exception.WxPayException: 发生异常,解密退款通知加密信息时出错
        at com.github.binarywang.wxpay.service.impl.WxPayServiceAbstractImpl.parseRefundNotifyResult(WxPayServiceAbstractImpl.java:135) ~[weixin-java-pay-2.9.0.jar:na]
        at com.xhlcgy.weixin.controller.WxPayController.parseRefundNotifyResult(WxPayController.java:290) ~[classes/:na]
        at 
Caused by: java.security.InvalidKeyException: Invalid AES key length: 31 bytes
        at com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:87) ~[sunjce_provider.jar:1.8.0_112]
        at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:94) ~[sunjce_provider.jar:1.8.0_112]
        at com.sun.crypto.provider.CipherCore.init(CipherCore.java:591) ~[sunjce_provider.jar:1.8.0_112]
        at com.sun.crypto.provider.CipherCore.init(CipherCore.java:467) ~[sunjce_provider.jar:1.8.0_112]
        at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313) ~[sunjce_provider.jar:1.8.0_112]
        at javax.crypto.Cipher.implInit(Cipher.java:802) ~[na:1.8.0_121]
        at javax.crypto.Cipher.chooseProvider(Cipher.java:864) ~[na:1.8.0_121]
        at javax.crypto.Cipher.init(Cipher.java:1249) ~[na:1.8.0_121]
        at javax.crypto.Cipher.init(Cipher.java:1186) ~[na:1.8.0_121]
        at com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult.fromXML(WxPayRefundNotifyResult.java:53) ~[weixin-java-pay-2.9.0.jar:na]
        ... 74 common frames omitted

在退款成功的通知消息的解析中的WxPayRefundNotifyResult.fromXML(xmlData, this.getConfig().getMchKey())方法中一直出现这个异常,最后发现是mchKey,经过加密之后的字符串是0xxxxxx,开头的,在通过
final String keyMd5String = new BigInteger(1, md5.digest()).toString(16).toLowerCase();
上面的bigInteger构造自动将 首位的0省略掉,所以字符串是31位,AES才会报错,所以建议更换其他方式转换成32位字符串

@binarywang
Copy link
Member

binarywang/weixin-java-pay-demo#21 麻烦帮忙验证下该问题楼主的代码能否修复你所遇到的问题?

@binarywang
Copy link
Member

3.1.4.BETA测试版本 已修复,楼主可以验证下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants