内容纲要
背景
在开发环境运行得很正常的 AES 加解密,在生产环境报异常
Caused by: java.security.InvalidAlgorithmParameterException: AlgorithmParameterSpec not of GCMParameterSpec
at java.base/com.sun.crypto.provider.GaloisCounterMode.engineInit(GaloisCounterMode.java:292)
at java.base/javax.crypto.Cipher.implInit(Cipher.java:876)
at java.base/javax.crypto.Cipher.chooseProvider(Cipher.java:934)
at java.base/javax.crypto.Cipher.init(Cipher.java:1466)
at java.base/javax.crypto.Cipher.init(Cipher.java:1393)
网上搜索了一番,基本结论是 jdk 版本问题:编译打包,我用的阿里云效,jdk 版本是17,运行环境是 jdk21;而开发环境只有 jdk21,就不会出错
既然这样,那把运行环境的 jdk 降级到 17 应该就可以解决了?试下吧
降级到 jdk17
先查下 jdk 版本
java -version
java version "21.0.1" 2023-10-17 LTS
卸载
查询安装包
rpm -qa | grep jdk
jdk-21-21.0.1-12.x86_64
卸载
rpm -e jdk-21-21.0.1-12.x86_64
安装 jdk17
下载
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm
安装
rpm -ivh jdk-17_linux-x64_bin.rpm
检查 java 版本
java --version
java 17.0.9 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 17.0.9+11-LTS-201)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)
更新环境变量
在 ~/.bashrc
最后添加一行
export JAVA_HOME=/usr/java/jdk-17/
执行
source ~/.bashrc
新的问题
这次运行后,抛出了新的异常
......
Caused by: java.lang.SecurityException: JCE cannot authenticate the provider BC
at java.base/javax.crypto.Cipher.getInstance(Cipher.java:722)
at com.refusea.commons.utils.PrivacyProtector$MyCipher.encryptHex(PrivacyProtector.java:66)
... 131 common frames omitted
Caused by: java.lang.IllegalStateException: zip file closed
at java.base/java.util.zip.ZipFile.ensureOpen(ZipFile.java:840)
at java.base/java.util.zip.ZipFile.getManifestName(ZipFile.java:1066)
at java.base/java.util.zip.ZipFile$1.getManifestName(ZipFile.java:1125)
......
这个问题我折腾好久,最后确认是 jdk17 的 bug,换装 openjdk17 可以修复
那么先卸载 jdk17,安装 openjdk17,这次我用 yum 安装
先查下安装包名
yum search openjdk
安装
yum install java-17-openjdk.x86_64
安装完成后,修改 JAVA_HOME 环境变量,在 ~/.bash.rc
最后一行添加
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/
查看 java 版本
java -version
openjdk version "17.0.1" 2021-10-19 LTS
OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS)
OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing)
重新执行程序,AES 加解密正常,搞定收工
AES 加密的奇怪异常