java.security.InvalidKeyException: Illegal key size aes解密失败
使用微信时定期提示:java.security.InvalidKeyException: Illegal key size和
com.qq.weixin.mp.aes.AesException: aes解密失败
at com.qq.weixin.mp.aes.WXBizMsgCrypt.decrypt(WXBizMsgCrypt.java:169)
at com.qq.weixin.mp.aes.WXBizMsgCrypt.decryptMsg(WXBizMsgCrypt.java:264)
at cn.pdmi.channel.controller.WechatController.authWechat(WechatController.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
这是因为用到了jdk sercurity, 而jdk sercurity 使用的jar包 $JAVA_HOME/lib/security/local_policy.jar 和 $JAVA_HOME/jre6_64/lib/security/US_export_policy.jar中有对技术出口的限制,加密只有128bit,而没有限制的则有256bit.
解决办法:
1. 按照版本选择下载 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8
2. 下载jce_policy-1_8_2.zip后解压,放入$JAVA_HOME/lib/security/目录下替换原来的jar包。
3. 重启服务