微信 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)

問題

問題日誌
最近在遷移的服務器,在遷移完以後, 一個有關微信小程序的日誌打印下面的報錯信息。html

c.t.b.a.c.weixin.aes.WXBizMsgCrypt - 小程序解密異常
java.security.InvalidKeyException: Illegal key size

解密失敗,看了下解密的密鑰是正確的,沒有任何問題。 這個在 經典 下是能夠運行的,在 VPC 下運行不了。 (由於最近在進行阿里雲網絡遷移)java

問題緣由

微信在進行數據傳輸的時候,會進行加密,微信使用的 AES 加密使用的是 256位,Java 默認使用的解密包是 local_policy.jarUS_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的值來改變這個默認的值。

相關文章
相關標籤/搜索