0

I am trying to encrypt some data using AES 256 bit encryption however I keep getting the error -

Error while encrypting: java.security.InvalidKeyException: Illegal key size or default parameters

My code is -

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);

Can someone please help figure out why the error is coming. It is working if I make it into 128 bit instead of 256 bit.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
AP01
  • 47
  • 1
  • 2
  • 9

1 Answers1

1

By default Java supports only 128-bit encryption. If you want to exceed more than that you need the unlimited strength file installed. To do that download the jars and extract the jar files from the zip and save them in ${java.home}/jre/lib/security/.

For more see here: https://stackoverflow.com/a/6481658/1008278

Community
  • 1
  • 1
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42