分類
發燒車訊

微信 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的值來改變這個默認的值。

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
【其他文章推薦】

USB CONNECTOR掌控什麼技術要點? 帶您認識其相關發展及效能

※評比前十大台北網頁設計台北網站設計公司知名案例作品心得分享

※智慧手機時代的來臨,RWD網頁設計已成為網頁設計推薦首選

台灣海運大陸貨務運送流程

兩岸物流進出口一站式服務