aes祕鑰限制問題解決辦法
在oarcle jdk1.8上執行256位的aes祕鑰加密報錯如下:
java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size
at com.xx.opal.core.utils.aes.AESTool.encryptWithoutEncode(AESTool.java:33)
at com.xx.opal.aes.AESTest.test(AESTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
解決方法:
方式1:新增無限制配置
策略檔案下載地址(jar包)
JDK6的下載地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
JDK7的下載地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK8的下載地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
下載後解壓,可以看到local_policy.jar和US_export_policy.jar以及readme.txt。
如果安裝了JRE,將兩個jar檔案放到%JRE_HOME%\lib\security下覆蓋原來檔案,記得先備份。
如果安裝了JDK,將兩個jar檔案也放到%JDK_HOME%\jre\lib\security下。
方式2:使用openjdk版本(1.8及以下版本)
例如:
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
方式3:使用1.9及以上jdk版本
方式4:使用反射接觸限制
方式5:使用bc非標準api
Bouncy Castle,三方類庫通常包含JCE標準實現,通過新增第三方Provider後使用JCE任然不能解決問題
方式6:設定crypto.policy屬性(1.8版本)
Security.setProperty("crypto.policy", "unlimited");
結論
最終採用了設定crypto.policy屬