微信 AES 解密报错 Illegal key size 三种解决办法
微信 AES 解密报错 Illegal key size
Java 环境
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
问题
问题日志
最近在迁移的服务器,在迁移完之后, 一个有关微信小程序的日志打印下面的报错信息。
c.t.b.a.c.weixin.aes.WXBizMsgCrypt - 小程序解密异常
java.security.InvalidKeyException: Illegal key size
解密失败,看了下解密的密钥是正确的,没有任何问题。 这个在 经典 下是可以运行的,在 VPC 下运行不了。 (因为最近在进行阿里云网络迁移)
问题原因
微信在进行数据传输的时候,会进行加密,微信使用的 AES 加密使用的是 256位,Java 默认使用的解密包是 local_policy.jar 和 US_export_policy.jar,但是这个默认的只支持 128位的解密(java 版本在 1.8.0_161之后就没有这个问题了,默认是支持)。我们的版本是 1.8.0_151 正好默认是只支持 128位的解密(其实不是不支持,只是默认配置的不支持)。
那么在哪些场景会用到 256位解密的呢?
- 微信发送消息(当使用了安全模式)

- 微信退款

解决办法
在前面我们没有提及一个东西,就是在/usr/local/java/jdk1.8.0_151/jre/lib/security/policy/下有两个目录。
[root@djx-117106 policy]# pwd
/usr/local/java/jdk1.8.0_151/jre/lib/security/policy/
[root@djx-117106 policy]# ls -l
total 8
drwxr-xr-x 2 root root 4096 Nov 2 10:47 limited
drwxr-xr-x 2 root root 4096 Nov 2 10:47 unlimited
[root@djx-117106 policy]# ls -l ./limited/
total 8
-rw-r--r-- 1 root root 3405 Jul 4 19:41 local_policy.jar
-rw-r--r-- 1 root root 2920 Jul 4 19:41 US_export_policy.jar
[root@djx-117106 policy]# ls -l ./unlimited/
total 8
-rw-r--r-- 1 root root 2929 Jul 4 19:41 local_policy.jar
-rw-r--r-- 1 root root 2917 Jul 4 19:41 US_export_policy.jar
有一个 limited 目录(也就是对解密有限制的包,只支持 128位),也有一个 ulimited 目录(也就是没有限制的目录)。
更改 源码
我们在 /usr/local/java/jdk1.8.0_151/jre/lib/security/ 下的 java.security文件中看到。
# To support older JDK Update releases, the crypto.policy property
# is not defined by default. When the property is not defined, an
# update release binary aware of the new property will use the following
# logic to decide what crypto policy files get used :
#
# * If the US_export_policy.jar and local_policy.jar files are located
# in the (legacy) <java-home>/lib/security directory, then the rules
# embedded in those jar files will be used. This helps preserve compatibility
# for users upgrading from an older installation.
#
# * If crypto.policy is not defined and no such jar files are present in
# the legacy locations, then the JDK will use the limited settings
# (equivalent to crypto.policy=limited)
#
# Please see the JCA documentation for additional information on these
# files and formats.
#crypto.policy=unlimited
注意下文中的 (equivalent to crypto.policy=limited) 说明默认是使用的 limited.
我们只需要加 crypto.policy=unlimited. 让默认使用的不限制的。
替换Jar包
替换 /usr/local/java/jdk1.8.0_151/jre/lib/security/policy/limited的路径的包。其实我们可以直接用 /usr/local/java/jdk1.8.0_151/jre/lib/security/policy/unlimited下面的包直接替换 /usr/local/java/jdk1.8.0_151/jre/lib/security/policy/limited/ 下面的两个包。也就是让默认使用不限制的jar包。
升级 Java 版本
https://www.oracle.com/technetwork/java/javase/8u161-relnotes-4021379.html
在官方文档写到,
security-libs/javax.crypto
Unlimited cryptography enabled by default
The JDK uses the Java Cryptography Extension (JCE) Jurisdiction Policy files to configure cryptographic algorithm restrictions. Previously, the Policy files in the JDK placed limits on various algorithms. This release ships with both the limited and unlimited jurisdiction policy files, with unlimited being the default. The behavior can be controlled via the new 'crypto.policy' Security property found in the /lib/java.security file. Please refer to that file for more information on this property.
也就是从 1.8.0_161-b12 版本后,默认将采用无限制的加密算法,也就是使用 unlimited 下的jar包。我们也可以通过 设置 java.security 文件的 crypto.policy的值来改变这个默认的值。
微信 AES 解密报错 Illegal key size 三种解决办法的更多相关文章
- 微信退款异步通知报错Illegal key size or default parameters 的解决办法
问题原因: Java几乎各种常用加密算法都能找到对应的实现.因为美国的出口限制,Sun通过权限文件(local_policy.jar.US_export_policy.jar)做了相应限制.因此存在一 ...
- 报错 Illegal key size or default parameters
简介: java中使用AES对称加密后,请求报错: Caused by: java.lang.RuntimeException: java.security.InvalidKeyException: ...
- Oracle 12c报错:ORA-01078和LRM-00109的解决办法
Oracle 12c报错:ORA-01078和LRM-00109的解决办法 2017-12-17 10:25:30 lemon_love1 阅读数 4336 收藏 更多 分类专栏: oracle ...
- java中的AES 256算法遇到 Illegal key size or default parameters错的解决办法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- Java企业微信开发_Exception_02_java.security.InvalidKeyException: Illegal key size
今天换了重新装了一个jdk,然后运行昨天还好好的企业微信工程,结果启动的时候就给我报了这么个错: java.security.InvalidKeyException: Illegal key size ...
- Java 解密错误InvalidKeyException: Illegal key size解决方法
做解密操作,出现如下错误 java.security.InvalidKeyException: Illegal key size // 设置解密模式为AES的CBC模式 Cipher cipher = ...
- yum本地安装rrdtool报错,提示版本冲突的解决办法
[root@mysql-mon40 mm]# yum localinstall rrdtool-1.4.7-1.el6.rfx.x86_64.rpm -y 错误: Multilib version p ...
- sql注入 报错注入常用的三种函数
1.floor()函数 报错原因是 报错的原因是因为rand()函数在查询的时候会执行一次,插入的时候还会执行一次.这就是整个语句报错的关键 前面说过floor(rand(0)*2) 前六位是0110 ...
- PHP安装mcrypt.so报错 mcrypt.h not found 的解决办法
报错内容:configure: error: mcrypt.h not found. Please reinstall libmcrypt 网上搜索了很多,包括自带的 yum install libm ...
随机推荐
- .Net Core API使用ODP.NET操作Oracle数据库
.Net Core API使用ODP.NET操作Oracle数据库 1.下载Oracle.ManagerDataAccess.Core. 右键依赖项——管理NuGet程序包. 在浏览选项中查询Orac ...
- PHP7源码之array_unique函数分析
以下源码基于 PHP 7.3.8 array array_unique ( array $array [, int $sort_flags = SORT_STRING ] ) (PHP 4 >= ...
- Grid实现table表格布局
HTML <div class="table"> <div class="th"> <div>日期</div> ...
- Vulnhub靶场渗透练习(五) Lazysysadmin
第一步扫描ip nmap 192.168.18.* 获取ip 192.168.18.147 扫描端口 root@kali:~# masscan - --rate= Starting massc ...
- 1.7.3.1版本ride乱码的解决方法
现象: 解决方式: 修改文件\Python36\Lib\site-packages\robotide\contrib\testrunner\testrunner.py 将latin1修改为mbcs 然 ...
- kubernetes kubelet组件中cgroup的层层"戒备"
cgroup是linux内核中用于实现资源使用限制和统计的模块,docker的风靡一时少不了cgroup等特性的支持.kubernetes作为容器编排引擎,除了借助docker进行容器进程的资源管理外 ...
- InitializingBean,spring 初始化bean
springframework的提供接口,InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的 ...
- UIAlert
转自:https://blog.csdn.net/deng0zhaotai/article/details/53887508 通过uialertcontroller实现三种简易弹框 (一)警告类 - ...
- rem辅助式响应布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- List、Set集合系列之剖析HashSet存储原理(HashMap底层)
目录 List接口 1.1 List接口介绍 1.2 List接口中常用方法 List的子类 2.1 ArrayList集合 2.2 LinkedList集合 Set接口 3.1 Set接口介绍 Se ...