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?
the AES is only for a fixed block length of 128 bit (16 byte). The key length can be choosen from 128 bit, 192 bit, 256 bit.
Advanced Encryption Standard (AES) keys are symmetric keys that can be three different key lengths (128, 192, or 256 bits). AES is the encryption standard that is recognized and recommended by the US government. The 256-bit keys are the longest allowed by AES.
Since AES supports three key sizes, we should choose the right key size for the use case. AES-128 is the most common choice in commercial applications. It offers a balance between security and speed. National Governments typically make use of AES-192 and AES-256 to have maximum security.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With