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.

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的更多相关文章

  1. Java学习-050-AES256 之 java.security.InvalidKeyException: Illegal key size or default parameters 解决方法

    在进行 Java AES 加密测试时,出现如下错误信息: java.security.InvalidKeyException: Illegal key size or default paramete ...

  2. AES的256位密钥加解密报 java.security.InvalidKeyException: Illegal key size or default parameters 异常的处理及处理工具

    一.出现的现象为了数据代码在传输过程中的安全,很多时候我们都会将要传输的数据进行加密,然后等对方拿到后再解密使用.我们在使用AES加解密的时候,在遇到128位密钥加解密的时候,没有进行什么特殊处理:然 ...

  3. java.security.InvalidKeyException: Illegal key size or default parameters

    今天在使用idea打包maven项目时,出现这个错误:java.security.InvalidKeyException: Illegal key size or default parameters ...

  4. 加密时java.security.InvalidKeyException: Illegal key size or default parameters解决办法

    需 Java几乎各种常用加密算法都能找到对应的实现.因为美国的出口限制,Sun通过权限文件(local_policy.jar.US_export_policy.jar)做了相应限制.因此存在一些问题: ...

  5. AES 加密256位 错误 java.security.InvalidKeyException: Illegal key size or default parameters

    Java发布的运行环境包中的加解密有一定的限制.比如默认不允许256位密钥的AES加解密,解决方法就是修改策略文件. 官方网站提供了JCE无限制权限策略文件的下载: JDK8的下载地址: http:/ ...

  6. 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 ...

  7. AES加密时抛出java.security.InvalidKeyException: Illegal key size or def

    原文:AES加密时抛出java.security.InvalidKeyException: Illegal key size or def 使用AES加密时,当密钥大于128时,代码会抛出 java. ...

  8. AES加密时的 java.security.InvalidKeyException: Illegal key size 异常

    程序代码 // 设置加密模式为AES的CBC模式 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKe ...

  9. java.security.InvalidKeyException: Illegal key size

    今天遇到一个奇怪的问题. 自己做的加签验签功能已经没有问题了,本地测试通过,同事放到服务器上测试也没问题. 然后我将包放到自己搭建的环境上,会报这样一个错误: java.security.Invali ...

随机推荐

  1. 网页前端 html js 相关

    1.注释 1.1HTML 注释 http://www.w3school.com.cn/html/html_comments.asp 注释标签 <!-- 与 --> 用于在 HTML 插入注 ...

  2. JS如何监听动画结束

    场景描述 在使用JS控制动画时一般需要在动画结束后执行回调去进行DOM的相关操作,所以需要监听动画结束进行回调.JS提供了以下事件用于监听动画的结束,简单总结学习下. CSS3动画监听事件 trans ...

  3. grails服务端口冲突解决办法-【grails】

    grails中默认的服务端口为,当本机中需要同时启动两个不同的项目时,就会造成端口冲突,比如启动第二个服务时就会报如下的错误: Server failed to start for port 8080 ...

  4. JMeter中BeanShell断言使用一

    Jmeter Ant Task如何让beanshell断言失败的详细信息展示在report里面 首先必须给beanshell断言添加FailureMessage if(${TotalClient_SS ...

  5. 使用spark集成kudu做DDL

    spark对kudu表的创建 定义kudu的表需要分成5个步骤: 1:提供表名 2:提供schema 3:提供主键 4:定义重要选项:例如:定义分区的schema 5:调用create Table a ...

  6. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第十三集之Redis的单机版搭建】

    (转载其他博客的安装步骤,截图是自己的) 1, 第一步:安装gcc编译环境 yum install gcc-c++ 第二步:把redis的源码上传到linux服务器. 第三步:解压缩. tar -zx ...

  7. 学习Spring必须了解的基础知识——回调机制

    上面这张图如果能看得懂就能理解什么是回调机制: A对象在调用a()方法时会调用B对象的b()方法,b()方法必须能调用A对象的callback()方法. 谁白了:a()方法有B对象b()方法的引用,b ...

  8. react-antd 按需加载报错

    基于create-react-app 搭建的 react 项目 引入  antd UI  配置按需加载 但是报一下错误 .翻译过了一下 是内嵌JavaScript选项没有开启什么的 大白话就是 les ...

  9. VDOM总结

    https://segmentfault.com/a/1190000016129036

  10. Codeforces 853B Jury Meeting (差分+前缀和)

    <题目链接> 题目大意: 有$ n(n<=1e5)$个城市和一个首都(0号城市),现在每个城市有一个人,总共有$ m (m<=1e5)$次航班,每个航班要么从首都起飞,要么飞到 ...