Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid AES key length: 128 bytes?

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?

like image 847
vrwim Avatar asked Apr 30 '14 15:04

vrwim


People also ask

How many bytes is an AES 128 key?

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.

What is valid AES key length?

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.

What is a good AES key?

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.


1 Answers

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.

like image 196
Konstantin V. Salikhov Avatar answered Sep 27 '22 18:09

Konstantin V. Salikhov