线程“main”中的异常java.security.InvalidKeyException:非法的密钥大小或默认参数 [英] Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters
本文介绍了线程“main”中的异常java.security.InvalidKeyException:非法的密钥大小或默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码抛出此错误消息:
The code belows is throwing this error message:
线程main中的异常java.security.InvalidKeyException:非法密钥大小或默认值参数
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?
推荐答案
可能您还没有安装JCE策略文件。
Probably you did not install the JCE Policy file yet.
下载此文件:
并在 $ {java.home} / jre / lib / security /中安装文件。
$ {java.home}
指的是Java的安装目录
${java.home}
refers to your installation directory of Java
for mac:
- open finder
- 按命令 + shift + g
- 类型
/ Library / Java / JavaVirtualMachines
- 导航到您的JDK版本
- 然后
目录/主页/ jre / lib / security
- 解压缩下载的文件并将所有文件放入ide here
- 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
用于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>/
这篇关于线程“main”中的异常java.security.InvalidKeyException:非法的密钥大小或默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文