I'm trying to implement encrypt with AES/128/CBC, but I need to encrypt with a 64 bytes key, But I have the problem with the length.
It is possible encrypt AES/128/CBC with a 64 bytes key or not, because I have the cipher algoritm in php with a 64 bytes key and it works, but not in java.
This is the encrypt code:
public static String ALGORITHM = "AES";
public static String AES_CBC_PADDING = "AES/CBC/PKCS5Padding";
public static byte[] encrypt(final byte[] key, final byte[] IV, final byte[] message) throws Exception {
return AESManager.encryptDecrypt(Cipher.ENCRYPT_MODE, key, IV, message);
}
public static byte[] decrypt(final byte[] key, final byte[] IV, final byte[] message) throws Exception {
return AESManager.encryptDecrypt(Cipher.DECRYPT_MODE, key, IV, message);
}
private static byte[] encryptDecrypt(final int mode, final byte[] key,
final byte[] IV, final byte[] message) throws Exception {
final Cipher cipher = Cipher.getInstance(AES_CBC_PADDING);
final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
final IvParameterSpec ivSpec = new IvParameterSpec(IV);
cipher.init(mode, keySpec, ivSpec);
return cipher.doFinal(message);
}
public static String getHex(byte[] data, int length) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
String hexStr = Integer.toHexString(((int) data[i]) & 0xFF);
if (hexStr.length() < 2) {
sb.append("0").append(hexStr.toUpperCase());
} else {
sb.append(hexStr.toUpperCase());
}
}
return sb.toString();
}
I have this error:
java.security.InvalidKeyException: Invalid AES key length: 64 bytes
at com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:87)
at com.sun.crypto.provider.CipherBlockChaining.init(CipherBlockChaining.java:93)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:591)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:346)
at javax.crypto.Cipher.implInit(Cipher.java:805)
at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
at javax.crypto.Cipher.init(Cipher.java:1395)
at javax.crypto.Cipher.init(Cipher.java:1326)
at ec.otecel.tuenti.balance.util.AESManager.encryptDecrypt(AESManager.java)
at ec.otecel.tuenti.balance.util.AESManager.encrypt(AESManager.java)
at ec.otecel.tuenti.balance.util.FreemiumUtil.ciphertoFreemium(FreemiumUtil.java)
at ec.otecel.tuenti.balance.util.FreemiumUtil.main(FreemiumUtil.java)
I have local_policy.jar and US_export_policy.jar in C:\Program Files\Java\jdk1.8.0_241\jre\lib\security so, what could be the problem ?