【发布时间】:2013-11-20 07:14:20
【问题描述】:
下面的代码抛出这个错误信息:
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;
有人知道我为什么会收到这个错误吗?
【问题讨论】:
-
异常在哪一行被抛出?请标记
-
您是否安装了
Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy File?除非将该文件安装到您的计算机上,否则无法使用 256 位 AES(来自 Java 加密包)。 -
对于这个问题我不得不下载
Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8