I'm traying to implement AES/ECB using 192 bit key however, my cipher.init
dose not allow that.
the only thing works is 128 bit key. I have tired Generate the key using "KeyGenerator" but the key Generated is 128 bit and when I try to pass 192 as a key size to the keyGenerator.init(192);
or when I try to enter the key manually as Hex or normal string converted to bytes nothing is working unless I write 128bit key. every thread or guide I have found for AES using java is just working on 128 and mention that it can works on 128, 192, or 256 bit.
The code:
package javaapplication13;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class JavaApplication13 {
public static void main(String[] args) throws Exception {
String text = "012345678901234567890123"; //192 bit key >> 24 length String
byte[] bytes = text.getBytes();
SecretKeySpec sks = new SecretKeySpec(bytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
System.out.println("sds");
cipher.init(Cipher.ENCRYPT_MODE, sks);
System.out.println("sds");
byte[] cipherText = cipher.doFinal("Hello World".getBytes());
System.out.println(new String(cipherText));
}
}
The Output:
Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1026)
at javax.crypto.Cipher.implInit(Cipher.java:801)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1249)
at javax.crypto.Cipher.init(Cipher.java:1186)
at javaapplication13.JavaApplication13.main(JavaApplication13.java:16)