1. 程式人生 > >微信 AES 解密報錯 Illegal key size 三種解決辦法

微信 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.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包

替換 /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的值來改變這個預設的值