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)
问题日志
最近在迁移的服务器,在迁移完以后, 一个有关微信小程序的日志打印下面的报错信息。html
c.t.b.a.c.weixin.aes.WXBizMsgCrypt - 小程序解密异常 java.security.InvalidKeyException: Illegal key size
解密失败,看了下解密的密钥是正确的,没有任何问题。 这个在 经典 下是能够运行的,在 VPC 下运行不了。 (由于最近在进行阿里云网络迁移)java
微信在进行数据传输的时候,会进行加密,微信使用的 AES 加密使用的是 256位,Java 默认使用的解密包是 local_policy.jar
和 US_export_policy.jar
,可是这个默认的只支持 128位的解密(java 版本在 1.8.0_161以后就没有这个问题了,默认是支持)。咱们的版本是 1.8.0_151
正好默认是只支持 128位的解密(其实不是不支持,只是默认配置的不支持)。算法
在前面咱们没有说起一个东西,就是在/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包oracle
替换 /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包。ide
升级 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
的值来改变这个默认的值。