Question

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

CIPHER.init(Cipher.ENCRYPT_MODE, keySpec);

with CIPHER being

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

and keySpec

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

that key is a byte[] of length 128 I got through a Diffie-Hellman key exchange (though it shouldn't matter where I got it, right?), key is completely filled with nonzero bytes

Why is Cipher.init(...) complaining that the key is a wrong length? This webpage clearly states that a key of length 128 is supported.

What am I overlooking?

Was it helpful?

Solution

I think you need 128 bit key here for AES algorithm - not 128 byte. To convert your long key to needed length you could try something like password-based key derivation function. See PBKDF2 for example.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top