2010-12-10 15 views
9

Sto tentando di crittografare una stringa su Android con AES. La chiave simmetrica viene determinata in precedenza con l'algoritmo Diffie-Hellman e sembra essere ok (Key Length è 128 bit, vedi sotto).
Tuttavia, ho ottenere un codice InvalidKeyException: "Key length not 128/192/256 bits. "Crittografia AES: InvalidKeyException: lunghezza chiave non 128/192/256 bit

:

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(); 
    } 

il codice qui sopra porta alla seguente uscita:

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

Dopo di che, ho l'InvalidKeyException: Key length not 128/192/256 bits. Ma come si può vedere , la SecretKey ha una lunghezza di 128 bit!

Qualche idea?

risposta

19

La chiave generata è 128 byte, non 128 bit bit. "Lunghezza chiave" deve essere 16.

+0

uno mi ha battuto ad esso. @Peter: l'unico posto in cui troverai una proprietà Length che rappresenta il numero di bit sarà una raccolta di bit specializzata. Il 99,9% delle volte sarà un conteggio di caratteri o byte. –

+0

Hmm, hai ovviamente ragione. Quindi, utilizzando KeyAgreement.generateSecret ("AES") restituisce una chiave con una lunghezza di 128 byte. Ovviamente, è troppo ... Come posso ottenere una chiave con diciamo 256 bit? Grazie – Peter

+0

A proposito: Android utilizza BouncyCastle come Fornitore di sicurezza ... – Peter

1

Questa eccezione si verifica in genere a causa della lunghezza della chiave che hai passato per la crittografia. Se si utilizza la crittografia AES, il numero di caratteri deve essere lungo 128/192/256 bit . Ad esempio è possibile utilizzare la chiave di 16 caratteri, 24 caratteri o 32 caratteri.

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

Spero che questo aiuto ...

+0

C'è già una risposta accettata. –

Problemi correlati