AES encryption: InvalidKeyException: Key length not 128/192/256 bits
Asked Answered
R

2

22

I'm trying to encrypt a string on Android with AES. The symmetric key is determined previously with the Diffie-Hellman algorithm and seems to be ok (Key Length is 128 Bit, see below).
Nevertheless, I get a InvalidKeyException: "Key length not 128/192/256 bits."

Code:

KeyAgreement keyAgree = KeyAgreement.getInstance("DH", "BC");
keyAgree.init(this.smartphonePrivKey);
keyAgree.doPhase(serverPubKey, true);
SecretKey key = keyAgree.generateSecret("AES");
System.out.println("Key Length: " + key.getEncoded().length);
System.out.println("Key Algorithm: "+ key.getAlgorithm());
System.out.println("Key Format: "+ key.getFormat());

byte[] encrypted = null;
  Cipher cipher;
  try {
   cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
   System.out.println("Allowed Key Length: "
     + cipher.getMaxAllowedKeyLength("AES"));
   cipher.init(Cipher.ENCRYPT_MODE, key);
   encrypted = cipher.doFinal("YEAH".getBytes("UTF8"));
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }

The above Code leads to the following output:

_12-10 20:24:53.119: INFO/System.out(757): Key Length: 128_  
_12-10 20:24:53.119: INFO/System.out(757): Key Algorithm: AES_   
_12-10 20:24:53.119: INFO/System.out(757): Key Format: RAW_  
_12-10 20:24:53.470: INFO/System.out(757): Allowed Key Length: 2147483647_ 

After that, I get the InvalidKeyException: Key length not 128/192/256 bits. But as you can see, the SecretKey has a length of 128 Bits!

Any ideas?

Reece answered 10/12, 2010 at 20:29 Comment(0)
C
36

The key you generated is 128 bytes, not 128 bits. "Key Length" should be 16.

Couchman answered 10/12, 2010 at 21:2 Comment(5)
+1 Beat me to it. @Peter: The only place you'll find a Length property representing the number of bits will be a specialized bit collection. 99.9% of the time it will be a count of characters or bytes.Natalienatalina
Hmm, you're obviously right. So using KeyAgreement.generateSecret("AES") returns a key with a length of 128 Bytes. Obviously, that's too much... How can i obtain a key with let's say 256 Bit? ThanksReece
@Reece - If you are just exchanging messages with yourself, you can just use the first 16 bytes of the shared secret. Normally, the shared secret is hashed (along with some other information) to produce the actual symmetric key. If you need to inter-operate, you'll have to find out if you are supposed to use something like RFC 2631 to produce the keys. You also might have a problem in using ECB. It's only secure under limited conditions.Couchman
Hashing the shared secret with SHA-256 solved the problem. I can use this key now. I have to have a closer look at ECB though...Reece
you mean use this SecretKey key = keyAgree.generateSecret("ECB"); ?Enfleurage
T
15

This exception basically occur due to length of key that you hava passed for encryption.If you are using AES encryption then the number of characters must be in length of 128/192/256 bits. For example you can use the key of 16 character,24 character or 32 character.

String encrypted_data=AES.encrypt("HELLO","ASDFGHJKLASDFGHJ");

Hope this help...

Typist answered 31/1, 2017 at 5:47 Comment(1)
No, this expands and elaborates on the accepted answer. Upvote for the accepted answer and this append.Bysshe

© 2022 - 2024 — McMap. All rights reserved.