线程“main”中的异常 java.security.InvalidKeyException:非法密钥大小或默认参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19856324/
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-12 20:44:22  来源:igfitidea点击:

Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters

javajce

提问by user2968404

The code belows is throwing this error message:

下面的代码抛出此错误消息:

Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters

Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters

Cipher dcipher;

byte[] salt = new String("12345678").getBytes();
int iterationCount = 1024;
int keyStrength = 256;
SecretKey key;
byte[] iv;

Decrypter(String passPhrase) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory
            .getInstance("PBKDF2WithHmacSHA1");
    System.out.println("factory +" + factory);
    KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), salt,
            iterationCount, keyStrength);
    System.out.println("spec  " + spec);
    SecretKey tmp = factory.generateSecret(spec);
    System.out.println();
    key = new SecretKeySpec(tmp.getEncoded(), "AES");
    dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
}

public String encrypt(String data) throws Exception {
    dcipher.init(Cipher.ENCRYPT_MODE, key);
    AlgorithmParameters params = dcipher.getParameters();
    iv = params.getParameterSpec(IvParameterSpec.class).getIV();
    byte[] utf8EncryptedData = dcipher.doFinal(data.getBytes());
    String base64EncryptedData = new sun.misc.BASE64Encoder()
            .encodeBuffer(utf8EncryptedData);

    System.out.println("IV "
            + new sun.misc.BASE64Encoder().encodeBuffer(iv));
    System.out.println("Encrypted Data " + base64EncryptedData);
    return base64EncryptedData;

Does anybody know why I get that error?

有人知道我为什么会收到那个错误吗?

回答by CodeFanatic

Probably you did not install the JCE Policy file yet.

可能您还没有安装 JCE 策略文件。

Download this file:

下载此文件:

And Install the file in ${java.home}/jre/lib/security/.

并将文件安装在 ${java.home}/jre/lib/security/.

${java.home}refers to your installation directory of Java

${java.home}指的是你的Java安装目录

for mac:

对于 Mac:

  • open finder
  • press command+ shift+ g
  • type /Library/Java/JavaVirtualMachines
  • navigate to your version of JDK
  • then Contents/Home/jre/lib/security
  • unzip the downloaded file and place all files inside here
  • 打开取景器
  • command+ shift+g
  • 类型 /Library/Java/JavaVirtualMachines
  • 导航到您的 JDK 版本
  • 然后 Contents/Home/jre/lib/security
  • 解压缩下载的文件并将所有文件放在此处

for CLI

命令行界面

unzip downloaded_policy_file.zip  -d /Library/Java/JavaVirtualMachines/<JDK_VERSION>/Contents/Home/jre/lib/security/

mv /Library/Java/JavaVirtualMachines/<JDK_VERSION>/Contents/Home/jre/lib/security/UnlimitedJCEPolicyJDK<VERSION>/* /Library/Java/JavaVirtualMachines/<JDK_VERSION>/Contents/Home/jre/lib/security  

rm -rf Library/Java/JavaVirtualMachines/<JDK_VERSION>/Contents/Home/jre/lib/security/UnlimitedJCEPolicyJDK<VERSION>/

回答by Rajeev

For JAVA 7 the download link is jce-7-download

对于 JAVA 7,下载链接是jce-7-download

Copy the two downloaded jars in Java\jdk1.7.0_10\jre\lib\security Take a backup of older jars to be on safer side.

将下载的两个 jar 复制到 Java\jdk1.7.0_10\jre\lib\security 备份旧的 jar 以确保安全。

回答by Lucky

Download the JCE for Java 7 from this link http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

从此链接下载 Java 7 的 JCE http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

and open the path C:\Program Files\Java\jdk1.7.0_80\jre\lib\securityand paste the two jars here.(Even if the two jars were already present replace those two jars)

并打开路径C:\Program Files\Java\jdk1.7.0_80\jre\lib\security并将两个罐子粘贴到此处。(即使两个罐子已经存在,也请替换这两个罐子)

回答by Neftanic

In case you using Mac with homebrew

如果您使用带有自制软件的 Mac

brew cask install jce-unlimited-strength-policy

回答by Fredric Doddridge

As of JDK 1.8u151 it is not necessary to download the JCE libraries separately. Simply edit:

从 JDK 1.8u151 开始,不需要单独下载 JCE 库。简单编辑:

$JDK_HOME/jre/lib/security/java.security

and uncomment the line:

并取消注释该行:

crypto.policy=unlimited