2
我試圖將我的應用從128位AES密鑰升級到256位AES密鑰。但是,當我將第54行從128更改爲256時,出現以下密鑰大小錯誤。使用Java加密/解密AES 256密鑰
java.security.InvalidKeyException:非法密鑰大小
我已經正確安裝了JCE文件,由我的應用程序生成更長的密鑰的事實所證明。
package com.company;
import com.hazelcast.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.*;
class Encryption {
public static String encrypt (String strKey, String strIv, String str) {
String secret = "";
try{
byte[] key = Base64.decode(strKey.getBytes());
byte[] iv = Base64.decode(strIv.getBytes());
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
secret = new String(Base64.encode(cipher.doFinal(str.getBytes())));
}
catch(Exception e){
e.printStackTrace();
}
return secret;
}
public static String decrypt (String strKey, String strIv, String str) {
String secret = "";
try{
byte[] key = Base64.decode(strKey.getBytes());
byte[] iv = Base64.decode(strIv.getBytes());
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keyspec,ivspec);
secret = new String(cipher.doFinal(new Base64().decode(str.getBytes())));
}
catch(Exception e){
e.printStackTrace();
}
return secret;
}
public static void main(String[] argv) {
String strIv = "18A5Z/IsHs6g8/65sBxkCQ==";
String strKey = "";
int keyStrength = 256;
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(keyStrength);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
strKey = new String(new Base64().encode(raw));
System.out.println("Secret key is: " + strKey);
} catch (Exception ex) {
System.out.println(ex.toString());
}
String message = "My, it's a lovely day today!!!";
String encrypted = Encryption.encrypt(strKey, strIv, message);
System.out.println("Encrypted string is: " + encrypted);
System.out.println("Decrypted string is: " + Encryption.decrypt(strKey, strIv, encrypted));
}
}
我見過的其他帖子引用 「AES/CBC/PKCS7Padding」 加密方法,但只是讓我這個異常:
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
at javax.crypto.Cipher.getInstance(Cipher.java:540)
at com.company.Encryption.encrypt(Encryption.java:17)
at com.company.Encryption.main(Encryption.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
at javax.crypto.Cipher.getInstance(Cipher.java:540)
at com.company.Encryption.decrypt(Encryption.java:39)
at com.company.Encryption.main(Encryption.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
哪行引發InvalidKeyException? –
[InvalidKeyException非法密鑰大小]的可能重複(http://stackoverflow.com/questions/3862800/invalidkeyexception-illegal-key-size) –
*「我有JCE文件正確安裝」* - 如果您的意思是無限強度策略文件,你真的*確定你在安裝策略文件的JVM中運行代碼嗎?如果您不確定,那麼您應該將它們安裝在所有JRE中(包括全局JRE以及JDK內的JRE)。 –