Invalid AES key length: 128 bytes?

I get java.security.InvalidKeyException: Invalid AES key length: 128 bytes on my line

 CIPHER.init(Cipher.ENCRYPT_MODE, keySpec); 

with CIPHER

 Cipher CIPHER = Cipher.getInstance("AES"); 

and keySpec

 SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); 

this key is 128 byte[] , I went through the Diffie-Hellman key exchange (although it doesn't matter where I got it, right?), the key completely filled with non-zero bytes

Why Cipher.init(...) complain that the key is the wrong length? This web page clearly indicates that a key of length 128 is supported.

What am I missing?

+7
java encryption aes
source share
2 answers

I think you need a 128-bit key here for the AES algorithm, not 128 bytes. To convert the long key to the desired length, you can try something like a password-based key output function. For example, see PBKDF2.

+7
source share

The AES algorithm allows 128, 192 or 256 bit key lengths. which is 16, 24 or 32 bytes. The length of your keys should be 16, 24 or 32 bytes.

+1
source share

All Articles