Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top