java aes 256 java.security.InvalidKeyException:安装策略后密钥大小非法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20802380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:27:28  来源:igfitidea点击:

java aes 256 java.security.InvalidKeyException: Illegal key size after installation the policy

javaencryptionaessha256

提问by user2279172

i have a problem with the encrypt of the bytes with an AES 256 key. I already installed the policy. Here's what I've done:

我在使用 AES 256 密钥加密字节时遇到问题。我已经安装了策略。这是我所做的:

  1. download the file: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
  2. I moved the files local_policy and US_export_policy to the directory /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/lib/security
  3. i restart the mac
  1. 下载文件:http: //www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
  2. 我将文件 local_policy 和 US_export_policy 移动到目录 /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/lib/security
  3. 我重新启动了 mac

But still I get an error message with the following code:

但我仍然收到带有以下代码的错误消息:

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
SecretKey secretKey = keyGenerator.generateKey();
Cipher decryption = Cipher.getInstance("AES/CBC/PKCS5PADDING");
decryption.init(Cipher.DECRYPT_MODE, secretKey, 
new IvParameterSpec(secretKey.getEncoded())); // <-- Illegal key size

My Java version:

我的Java版本:

java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

What i have to do, to use the 256 AES encryption?

我必须做什么,才能使用 256 AES 加密?

采纳答案by Saran Makam

Policy files should resolve the issue. Check the path environment variable to confirm that you are using the same jre which has policy files.

策略文件应该可以解决这个问题。检查路径环境变量以确认您使用的是具有策略文件的相同 jre。

回答by hnrain

IMPORTANT: you have know you jre location. eg: in my compute have C:\Java\jre7\lib\securityand C:\Java\jdk1.7.0_02\jre\lib\security, but eclipse show the jre is C:\Java\jre7\lib\security,so you must copy two file into here.

重要提示:您已经知道您所在的位置。例如:在我的计算机中有C:\Java\jre7\lib\securityC:\Java\jdk1.7.0_02\jre\lib\security,但 eclipse 显示 jre 是C:\Java\jre7\lib\security,所以你必须将两个文件复制到这里。

回答by Lokesh Tiwari

What I did downloaded policy file from this location http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.htmlpaste in location jre/lib/security/and it workes....

我从这个位置下载了策略文件 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html粘贴到位置 jre/lib/security/并且它工作......