문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top