2012-11-20 5 views
5

Sto utilizzando AES 256 CBC. Ho 32 byte di IV. Ma quando eseguo questo mostra un'eccezione come:Desidera utilizzare CBC AES 256 con 32 byte ma mostra java.security.InvalidAlgorithmParameterException

Exception in thread "main" java.lang.RuntimeException: java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long 
    at com.abc.aes265cbc.AESUtil.decrypt(AESUtil.java:50) 
    at com.abc.aes265cbc.Security.main(Security.java:48) 
Caused by: java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long 
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:430) 
    at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:217) 
    at javax.crypto.Cipher.implInit(Cipher.java:790) 
    at javax.crypto.Cipher.chooseProvider(Cipher.java:848) 
    at javax.crypto.Cipher.init(Cipher.java:1347) 
    at javax.crypto.Cipher.init(Cipher.java:1281) 
    at com.abc.aes265cbc.AESUtil.decrypt(AESUtil.java:47) 
    ... 1 more 

Non so come risolvere questo. Ho cercato ma non ho trovato come risolvere questo. Sto cercando concetti di sicurezza per la prima volta. Il mio codice per l'AES 256 CBC è:

public static void setENCRYPTION_IV(String ENCRYPTION_IV) { 
     AESUtil.ENCRYPTION_IV = ENCRYPTION_IV; 
    } 

    public static void setENCRYPTION_KEY(String ENCRYPTION_KEY) { 
     AESUtil.ENCRYPTION_KEY = ENCRYPTION_KEY; 
    } 



    public static String encrypt(String src) { 
     try { 
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
      cipher.init(Cipher.ENCRYPT_MODE, makeKey(), makeIv()); 
      return Base64.encodeBytes(cipher.doFinal(src.getBytes())); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public static String decrypt(String src) { 
     String decrypted = ""; 
     try { 
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
      cipher.init(Cipher.DECRYPT_MODE, makeKey(), makeIv()); 
      decrypted = new String(cipher.doFinal(Base64.decode(src))); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
     return decrypted; 
    } 

    static AlgorithmParameterSpec makeIv() { 
     try { 
      return new IvParameterSpec(ENCRYPTION_IV.getBytes("UTF-8")); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    static Key makeKey() { 
     try { 
      MessageDigest md = MessageDigest.getInstance("SHA-256"); 
      byte[] key = md.digest(ENCRYPTION_KEY.getBytes("UTF-8")); 
      return new SecretKeySpec(key, "AES"); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

Può me che aiutare cambiando ciò che in questo codice sarò in grado di utilizzare 32 byte di IV. Grazie in anticipo

Edit: mia principale funzione che chiama queste funzioni:

AESUtil.setENCRYPTION_KEY("96161d7958c29a943a6537901ff0e913efaad15bd5e7c566f047412179504ffb"); 

    AESUtil.setENCRYPTION_IV("d41361ed2399251f535e65f84a8f1c57"); 
    String decrypted = AESUtil.decrypt(new String(sw0SrUIKe0DmS7sRd9+XMgtYg+BUiAfiOsdMw/Lo2RA=)); // AES Decrypt 
+0

È possibile fornire un [SSCCE] (http://sscce.org/) che è compilabile e eseguibile fuori dalla scatola e mostra il problema? (compreso un metodo principale che dimostra quali metodi si chiama in quale ordine) –

+0

Qual è il motivo per cui si desidera utilizzare 32 byte (256 bit)? Vedere la mia risposta aggiornata per 'AES/CBC/PKCS5Padding', blockLength = chiave Length = IV length = 128 bit (16 byte). –

+0

Ho aggiunto il mio valore di chiavi ... Questo mostrerà l'eccezione che ho ricevuto. – Vaibs

risposta

9

L'algoritmo AES ha una dimensione di blocco di 128 bit, indipendentemente dal fatto che la lunghezza della chiave è di 256, 192 o 128 bit.

Quando una modalità di crittografia simmetrica richiede un IV, la lunghezza della IV deve essere uguale alla dimensione del blocco del codice. Quindi, devi sempre usare una IV di 128 bit (16 byte) con AES.

Non c'è modo di utilizzare un IV di 32 byte con AES.

+1

Duncan, ho votato per eliminare la mia risposta dato che il vostro è molto chiaro e diretto :) Per i lettori futuri, vorrei solo mantenere il suggerimento che, per usare AES-256, è necessario [ installa i file di criteri senza limitazioni] (http://stackoverflow.com/questions/12897260/java-aes-256-decryption-translating-code-from-actionscript-3). Altrimenti si ottiene 'java.security.InvalidKeyException: La dimensione della chiave illegale 'anche per la chiave stessa, non solo per la IV. –

+1

@Andreas Grazie, molto gentile da parte tua. Mi scuso per aver rubato il tuo fulmine, ma volevo assicurarmi che una risposta succinta fosse disponibile per i futuri lettori. Il tuo punto sui file senza restrizioni è sempre buono da ricordare :-) –

+0

puoi aiutarmi con questi? :(http://stackoverflow.com/questions/34061675/convert-ios-encryption-to-android – MetaSnarf

Problemi correlati