Le chiffrement AES: InvalidKeyException: la longueur de la Clé pas 128/192/256 bits

Je suis en train de chiffrer une chaîne sur Android avec l'algorithme AES. La clé symétrique est déterminé précédemment avec l'algorithme Diffie-Hellman et semble être ok (la Longueur de Clé est de 128 Bits, voir ci-dessous).
Néanmoins, je reçois un 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();
  }

Le Code ci-dessus conduit à la sortie suivante:

_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_ 

Après, je reçois le InvalidKeyException: Key length not 128/192/256 bits. Mais comme vous pouvez le voir, le SecretKey a une longueur de 128 Bits!

Des idées?

OriginalL'auteur Peter | 2010-12-10