java jce配置_java JCE 不限密钥长度解决办法

java jce配置_java JCE 不限密钥长度解决办法()转自http://opensourceforgeeks.blogspot.com/2014/09/how-to-install-java-cryptography.html另外,在StackOverflow上也有相关讨论,并提供了反射实现代码https://stackoverflow.com/questions/25959948/local-policy-jar-and-us-export-p…

大家好,又见面了,我是你们的朋友全栈君。

()转自http://opensourceforgeeks.blogspot.com/2014/09/how-to-install-java-cryptography.html

另外,在StackOverflow上也有相关讨论,并提供了反射实现代码

https://stackoverflow.com/questions/25959948/local-policy-jar-and-us-export-policy-jar-different-with-unlimited-strength-vs-d

https://stackoverflow.com/questions/1179672/how-to-avoid-installing-unlimited-strength-jce-policy-files-when-deploying-an

前言

因为是需要,把英文原文复制了过来。

总结一下下面的内容:

1.java 8 161以上已经不再限制密钥长度

2.java 8 151以上,需要修改security文件,修改配置,重启jvm

3.java 8 151之前的,需要替换jar,重启jvm

4.不想替换jar,需要使用反射,来实现动态(作者不推荐)

How to install Java Cryptography Extension (JCE) unlimited strength jurisdiction policy files

Problem

JCE has been integrated into the Java 2 SDK since the 1.4 release.

Below diagram shows a general overview of Java cryptographic architecture. What we are discussing in this post is related to JCE implementation provided by Sun/Oracle.

As per the Oracle 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.

That mean JDK has a deliberate key size restriction by default. So you cannot perform an encryption with key more than 128 bits (16 bytes). If you do you will get an error something like –

Caused by: java.security.InvalidKeyException: Illegal key size or default parameters

If you get this Exception there is nothing wrong that you are doing. It’s just the restriction on the encryption key that comes built into the JDK.

The reason for this is that some countries have restrictions on the permitted key strength used in encryption algorithms.

Again as per the documentation –

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.

Update –  Updates Since Java 8 and Java 9

There have been multiple updates since Java 8 and 9. Before you dive down into more details review this section based on the Java version you are using. I have shown how to resolve this issue with various Java versions in the video below.

Java 9 and higher :

The Unlimited Strength Jurisdiction Policy Files are included with Java 9 and used by default (see Security Updates in the Java 9 Migration Guide).

If you get this error with Java 9, it might mean the policy configuration has been changed to a more restrictive policy (limited), see the instructions from the migration guide:

It states –

JCE Jurisdiction Policy File Default is Unlimited

If your application previously required the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files, then you no longer need to download or install them. They are included in the JDK and are activated by default.

If your country or usage requires a more restrictive policy, the limited Java cryptographic policy files are still available.

If you have requirements that are not met by either of the policy files provided by default, then you can customize these policy files to meet your needs.

See the crypto.policy Security property in the  /conf/security/java.security file, or Cryptographic Strength Configurationin the Java Platform, Standard Edition Security Developer’s Guide.

Java 8 Update 161 and higher

Starting with Java 8 Update 161, Java 8 defaults to the Unlimited Strength Jurisdiction Policy. If you receive this error, it could indicate the configuration has been changed to limited. See instructions in the next section on Java 8 Update 151, or the previous section on Java 9, for changing this back to unlimited.

Java 8 Update 151 and higher

Starting with Java 8 Update 151, the Unlimited Strength Jurisdiction Policy is included with Java 8 but not used by default. To enable it, you need to edit the java.security file in /jre/lib/security (for JDK) or /lib/security (for JRE). Uncomment (or include) the line

crypto.policy=unlimited

Make sure you edit the file using an editor run as administrator.

The policy change only takes effect after restarting the JVM (this is especially important for long-running server processes like Tomcat).

For backward compatibility, installing the policy files as documented in the next section will still work as well.

Before Java 8 Update 151 – Removing the maximum key size restriction

You can remove the maximum key restriction by replacing the existing JCE jars with unlimited strength policy jars.

Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6

Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7 Download

Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8 Download

Download the zip file extract the jars and replace them in your JDK/JRE.

For this Copy local_policy.jar and US_export_policy.jarextracted from above zip file to the $JAVA_HOME/jre/lib/security

Note: These jars will already be present there so you will have to overwrite them.

Then simply restart your java application and the Exception should be gone.

NOTE: If you are using Ubuntu and the webupd8 PPA, you can simply run –

apt-get install oracle-java8-unlimited-jce-policy

An alternate way to maximum encryption key size problem

This way is really a workaround. In fact, this approach is the workaround to all problems and it’s not straightforward. Yeah you must have guessed it by now – Reflection

You can override the restriction with Reflection as follows –

1

2

3

4

5

6

7

try {

Field field = Class.forName(“javax.crypto.JceSecurity”).getDeclaredField(“isRestricted”);

field.setAccessible(true);

field.set(null, java.lang.Boolean.FALSE);

}catch (Exception ex) {

ex.printStackTrace();

}

Note 1: I do not recommend the Reflection approach as it’s hacky. If you are using it keep it for testing only. Don’t put it in production code :)

Note 2:As the change of replacing policy files is in JDK itself you will have to do it in all your servers. Also, you will have to ask all your clients to do so.

Finding the maximum possible key length

To find maximum key length allowed by an encryption algorithm you can useCipher.getMaxAllowedKeyLength() method.  For example, for AES algorithm you can do –

int maxKeyLength = Cipher.getMaxAllowedKeyLength(“AES”);

Related Links

Oracle JCE documentation

How to avoid installing “Unlimited Strength” JCE policy files when deploying an application?

Java Security: Illegal key size or default parameters?

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150509.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【架构】Lambda架构

    【架构】Lambda架构一、出现的背景1.1从传统数据库到NoSQL,再到Hadoop很多人学习大数据都听说过以下发展进程,MySQL/Oracle/SQLServer→Hadoop/Hive/Spark。但还有一个时期,是大家容易忽略的——NoSQL。我们不能忽略掉它。其实,NoSQL的发展和推广要比Hadoop更早,在没有Hadoop的大数据过渡期,随着数据量急剧膨胀,大家纷纷从传统的关系型数据库转变到NoSQL数据库,各种各样的NoSQL数据库应用而生。有了NoSQL数据库,可以轻易将机器的数量扩展到.

    2022年6月25日
    35
  • html中图片自动循环滚动代码,实现长图片自动循环滚动效果[通俗易懂]

    html中图片自动循环滚动代码,实现长图片自动循环滚动效果[通俗易懂]实现思路滚动效果用实现。有个方法,可以滚动到指定位置(有滚动效果,不是直接到指定位置),不了解的看这里种定位滚动方式演示。每一个Item是一张长图,这样首尾相接滚动起来(滚到无限远)就是无限循环的效果,然后再改变滚动的速度,就可以了。{;@(){(savedInstanceState);//全屏getWindow().setFlags(WindowManager.LayoutParams.FLAG…

    2022年7月18日
    135
  • SpringCloud Eurake客户端操作相关笔记

    SpringCloud Eurake客户端操作相关笔记2019独角兽企业重金招聘Python工程师标准>>>…

    2022年6月2日
    31
  • 【STM32F407的DSP教程】第2章 Matlab R2018a的安装

    【STM32F407的DSP教程】第2章 Matlab R2018a的安装完整版教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=94547第2章MatlabR2018a的安装本期教程主要是讲解MatlabR2018a的安装过程,作为学习DSP的必备软件,掌握简单的Matlab操作是必须的。目录第2章MatlabR2018a的安装2.1初学者重要提示2…

    2025年9月24日
    8
  • 八大排序算法

    八大排序算法概述排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。我们这里说说八大排序就是内部排序。当n较大,则应采用时间复杂度为O(nlog2n)的排序方法:快速排序、堆排序或归并排序序。快速排序:是目前基于比较的内部排序中被认为是最好的方法,当待排序的关键字是随机分…

    2022年4月26日
    39
  • 【WPF】Toolkit(一个项目)的要点总结

    【WPF】Toolkit(一个项目)的要点总结架构相关1.插件式开发:MEF具体怎么使用可参考百度+Demo(密码:k8ck)2.备份机制(项目特有功能)待续3.镜像机制(项目特有功能)待续4.分模块记录日志(转)非常完善的Log4net详细说明UI相关1.多语言读取系统的显示语言(displayLanguage),显示语言的定义是:假如你的系统现在是中文的,

    2022年10月2日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号