以下代码抛出此错误消息:
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;
有人知道我为什么会收到这个错误吗?
答案 0 :(得分:23)
可能您尚未安装JCE策略文件。
下载此文件:
并在${java.home}/jre/lib/security/.
${java.home}
指的是Java的安装目录
for mac:
/Library/Java/JavaVirtualMachines
Contents/Home/jre/lib/security
用于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>/
答案 1 :(得分:1)
从此链接下载JCE for Java 7 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
并打开路径C:\Program Files\Java\jdk1.7.0_80\jre\lib\security
并在此处粘贴两个罐子。(即使两个罐子已经存在,也要替换这两个罐子)
答案 2 :(得分:0)
对于JAVA 7,下载链接为jce-7-download
将两个下载的jar复制到Java \ jdk1.7.0_10 \ jre \ lib \ security中 备份较旧的罐子以便更安全。
答案 3 :(得分:0)
如果您使用带自制软件的Mac
add_filter('single_template', 'check_for_category_single_template');
function check_for_category_single_template( $t ){
foreach( (array) get_the_category() as $cat ){
if ( file_exists(TEMPLATEPATH . "/single-category-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-category-{$cat->slug}.php";
if($cat->parent){
$cat = get_the_category_by_ID( $cat->parent );
if ( file_exists(TEMPLATEPATH . "/single-category-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-category-{$cat->slug}.php";
}
}
return $t;
}
答案 4 :(得分:0)
从JDK 1.8u151开始,没有必要单独下载JCE库。只需编辑:
$JDK_HOME/jre/lib/security/java.security
取消注释该行:
crypto.policy=unlimited