Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
How to remove the key size restriction in Java JDK?
Are you developing your beautiful application using the Java Cryptography Extension, and using a key length of more than 128 bits you encounter the following error?
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
There is nothing wrong that you are doing: JDK has a deliberate key size restriction by default, so you cannot use an encryption with key more than 128 bits.
From Oracle’s documentation:
Due to import control restrictions by the governments of a few countries, the jurisdiction policy files shipped with the JDK 5.0 from Sun Microsystems specify that “strong” but limited cryptography may be used.
Some countries have restrictions on the permitted key strength used in encryption algorithms:
An “unlimited strength” version of these files indicating no restrictions on cryptographic strengths is available for those living in eligible countries (which is most countries). But only the “strong” version can be imported into those countries whose governments mandate restrictions. The JCE framework will enforce the restrictions specified in the installed jurisdiction policy files.
How to remove the key size restriction?
You can remove the maximum key restriction by replacing the existing JCE jars with unlimited strength policy jars.
- For JAVA 7 the download link is jce-7-download
- For JAVA 8 the download link is jce-8-download
Copy local_policy.jar and US_export_policy.jar extracted from above zip file to the $JAVA_HOME/jre/lib/security
Then simply restart you java application and the Exception should be gone.
from:https://www.andreafortuna.org/java/java-tips-how-to-fix-the-invalidkeyexception-illegal-key-size-or-default-parameters-runtime/
如果不方便覆盖文件,可以在代码中通过反射修改,在main中调用一下方法就Ok了:
/**
* 去除JCE限制
*/
private static void removeJceLimit()
{
//去除JCE加密限制,只限于Java1.8
try {
Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, false); LOGGER.info("============= remove the key size restriction Success ============="); } catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
ex.printStackTrace(System.err);
}
}
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters的更多相关文章
- Java学习-050-AES256 之 java.security.InvalidKeyException: Illegal key size or default parameters 解决方法
在进行 Java AES 加密测试时,出现如下错误信息: java.security.InvalidKeyException: Illegal key size or default paramete ...
- AES的256位密钥加解密报 java.security.InvalidKeyException: Illegal key size or default parameters 异常的处理及处理工具
一.出现的现象为了数据代码在传输过程中的安全,很多时候我们都会将要传输的数据进行加密,然后等对方拿到后再解密使用.我们在使用AES加解密的时候,在遇到128位密钥加解密的时候,没有进行什么特殊处理:然 ...
- java.security.InvalidKeyException: Illegal key size or default parameters
今天在使用idea打包maven项目时,出现这个错误:java.security.InvalidKeyException: Illegal key size or default parameters ...
- 加密时java.security.InvalidKeyException: Illegal key size or default parameters解决办法
需 Java几乎各种常用加密算法都能找到对应的实现.因为美国的出口限制,Sun通过权限文件(local_policy.jar.US_export_policy.jar)做了相应限制.因此存在一些问题: ...
- AES 加密256位 错误 java.security.InvalidKeyException: Illegal key size or default parameters
Java发布的运行环境包中的加解密有一定的限制.比如默认不允许256位密钥的AES加解密,解决方法就是修改策略文件. 官方网站提供了JCE无限制权限策略文件的下载: JDK8的下载地址: http:/ ...
- AES加密时抛出java.security.InvalidKeyException: Illegal key size or default parametersIllegal key size or default parameters
使用AES加密时,当密钥大于128时,代码会抛出java.security.InvalidKeyException: Illegal key size or default parameters Il ...
- AES加密时抛出java.security.InvalidKeyException: Illegal key size or def
原文:AES加密时抛出java.security.InvalidKeyException: Illegal key size or def 使用AES加密时,当密钥大于128时,代码会抛出 java. ...
- AES加密时的 java.security.InvalidKeyException: Illegal key size 异常
程序代码 // 设置加密模式为AES的CBC模式 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKe ...
- java.security.InvalidKeyException: Illegal key size
今天遇到一个奇怪的问题. 自己做的加签验签功能已经没有问题了,本地测试通过,同事放到服务器上测试也没问题. 然后我将包放到自己搭建的环境上,会报这样一个错误: java.security.Invali ...
随机推荐
- 【Android】jar包Proguard混淆方法
本文章的前提条件是,读者已经掌握了正确导出jar包的技能. 1.拷贝Android项目中“proguard.cfg”文件到你指定的位置,并改名为“proguard.pro”,此步是为proguardg ...
- 分布式配置hadoop2.5.0 2.6.x
1. sudo vim /etc/hostname 在master的机器上,改成 master 在slave上写 slave01,02,03...... 配置好后重启. 2. sudo vi ...
- Zepto的使用以及注意事项
为什么选择Zepto.js的原因: zepto.js的语法借鉴并且兼容jQuery,会使用jquery就会使用Zepto.js.Zepto.js是移动端的js库.Zepto.js相当于PC端的j ...
- [转,讲的非常精彩]CIDR地址块及其子网划分(内含原始IP地址分类及其子网划分的介绍)
http://blog.csdn.net/dan15188387481/article/details/49873923 CIDR地址块及其子网划分(内含原始IP地址分类及其子网划分的介绍) 1. ...
- Idea中lombok不生效原因
我们可以通过在maven中插入配置信息 <dependency> <groupId>org.projectlombok</groupId> <artifact ...
- B. Divisiblity of Differences
B. Divisiblity of Differencestime limit per test1 secondmemory limit per test512 megabytesinputstand ...
- Git rebase命令实战
一.前言 一句话,git rebase 可以帮助项目中的提交历史干净整洁!!! 二.避免合并出现分叉现象 git merge操作 1.新建一个 develop 分支 2.在develop分支上新建 ...
- 前面的内容 也是要去掉白名单 和 8.8.8.8这种非问题IP的 高风险 么? (目前我们没有获取客户的中风险、低风险数据,可以处理掉高风险)
前面的内容 也是要去掉白名单 和 8.8.8.8这种非问题IP的 高风险 么? (目前我们没有获取客户的中风险.低风险数据,可以处理掉高风险) == 整体把关.不清楚细节,所以只能从整体决策.做 ...
- BZOJ.3992.[SDOI2015]序列统计(DP NTT 原根)
题目链接 \(Description\) 给定\(n,m,x\)和集合\(S\).求\(\prod_{i=1}^na_i\equiv x\ (mod\ m)\)的方案数.其中\(a_i\in S\). ...
- Java并发编程(六)-- 同步块
上一节已经讲到,使用Synchronzied代码块可以解决共享对象的竞争问题,其实还有其他的方法也可以避免资源竞争问题,我统称他们为Java同步块.Java 同步块(synchronized bloc ...