0

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)

  • 3
    Does this answer your question? [Java Security: Illegal key size or default parameters?](https://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters). Also, you should not decode the ciphertext with `new String(cipherText)`, but use a binary-to-text encoding like Base64. – Topaco Jul 23 '22 at 18:19
  • 1
    Your used Java version seems to be heavily outdated as it is still restricted to AES-128 because of the US export restrictions. Uninstall the old Java version and install a recent Java version 8, 11, 17 or 18 and everything is fine. – Robert Jul 24 '22 at 15:48

0 Answers0