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

a 夏天 提交于 2019-11-27 15:16:41

问题


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:

  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

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 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?


回答1:


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




回答2:


IMPORTANT: you have know you jre location. eg: in my compute have C:\Java\jre7\lib\security and 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.




回答3:


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



来源:https://stackoverflow.com/questions/20802380/java-aes-256-java-security-invalidkeyexception-illegal-key-size-after-installat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!