我試圖從PHP讀取base64編碼和AES 128位加密字符串,但我得到IllegalBlockSizeException。Java從PHP加密/解密數據到Java,IllegalBlockSizeException
PHP加密:
encrypt("My f awesome test !");
function encrypt($string){
$td = mcrypt_module_open('rijndael-128', '', 'cbc', "1cc251f602cf49f2");
mcrypt_generic_init($td, "f931c96c4a4e7e47", "1cc251f602cf49f2");
$enc = mcrypt_generic($td, $string);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return base64_encode($enc);
}
並且返回的值是:
McBeY73GQ5fawxIunVKpqUupipeRlt9ntyMRzjbPfTI =
現在我想用Java來閱讀:
static public String decrypt(String data) throws Exception {
data = new String(Base64.decode(data, Base64.NO_WRAP));
byte[] keyByte = "f931c96c4a4e7e47".getBytes("UTF-8");
byte[] ivByte = "1cc251f602cf49f2".getBytes("UTF-8");
Key key = new SecretKeySpec(keyByte, "AES");
IvParameterSpec iv = new IvParameterSpec(ivByte);
Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
c.init(Cipher.DECRYPT_MODE, key, iv);
byte[] bval = c.doFinal(data.getBytes("UTF-8"));
return new String(bval);
}
而且我得到一個例外:
javax.crypto.IllegalBlockSizeException:數據不是塊大小一致
這可能是由填充造成的?
EDIT
爲什麼密碼設置爲解密模式?還應該明確指出錯誤發生的位置以及輸入的內容。 – spinkus 2013-02-17 11:42:25