2017-02-09 197 views
0

我試圖加密使用AES 256位加密,但是我不斷收到錯誤一些數據 - 同時加密AES 256位加密

錯誤:java.security.InvalidKeyException:非法密鑰大小或默認參數

我的代碼是 -

key = "abcd123456789kjd"; 
byteKey = key.getBytes(); 
MessageDigest sha = MessageDigest.getInstance("SHA-256"); 
byteKey = sha.digest(byteKey); 
byteKey = Arrays.copyOf(byteKey, 32); // use only first 256 bit 
secretKey = new SecretKeySpec(byteKey, "AES"); 
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
cipher.init(Cipher.ENCRYPT_MODE, secretKey); 

有人可以幫忙弄清楚爲什麼錯誤即將到來。這是工作,如果我把它變成128位而不是256位。

回答

1

默認情況下,Java只支持128位加密。如果你想超過你需要安裝的無限強度文件。要做到這一點,請下載jar文件並從zip中提取jar文件並將它們保存在$ {java.home}/jre/lib/security /中。

詳細請看這裏:https://stackoverflow.com/a/6481658/1008278

+0

是的,這是問題的感謝! – AP01