2013-03-12 228 views
0

我想內置使用以該順序這個元素的字節的數組:初始化向量異常

  • 對稱加密的AES密鑰(與AES 128隨機密鑰,隨機初始化向量用於AES加密128.在CBC模式和PKCS5填充。加密前的文本在UTF-8編纂)
  • AES IV使用AES 128
  • 加密消息(使用ECB模式和PKCS1填充,RSA算法以前生成的密鑰和公共密鑰消息接收者)

我正在做的是獲取每個參數的長度,以創建新的字節[]。然後在for循環中,我嘗試按順序添加三個元素。

public void encrypt(String original) 
{ 
    SecureRandom sr = new SecureRandom(); 

    byte [] key = new byte[16]; 
    byte [] iv = new byte[16]; 

    sr.nextBytes(key); 
    sr.nextBytes(iv); 

    Cipher cipher; 

    try 
    { 
     cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
     IvParameterSpec IV=new IvParameterSpec(iv); 
     cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key,"AES"), IV); 
     byte[] utf8 = original.getBytes("UTF-8"); 
     byte []encryptedAES = cipher.doFinal(utf8); 

     KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); 
     kpg.initialize(128);//128 bits 
     KeyPair kp = kpg.genKeyPair(); 

     Cipher publicKeyCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); 
     publicKeyCipher.init(Cipher.ENCRYPT_MODE, kp.getPublic()); 
     byte [] encryptedRSA = publicKeyCipher.doFinal(encryptedAES); //error here 

     int length1=encryptedAES.length; 
     int length2=IV.getIV().length; 
     int length3=encryptedRSA.length; 
     int length=length1+length2+length3; 
     byte [] result= new byte[length]; 

     int l=0,m=0; 

     for (int i=0; i<length; i++) 
     { 
      if(i<length1) 
      { 
       result[i] = encryptedAES[i]; 
      } 
      else if(i>=length1 && i<length2) 
      { 
       result[i] = IV.getIV()[l]; 
       l++; 
      } 
      else if(i>=length2) 
      { 
       result[i] = encryptedRSA[m]; 
       m++; 
      } 
     } 
     Log.i("Encrypted", "done"); 
     this.encryptedMessage=Base64.encodeToString(result, false); 
     Log.i("Encrypted Message:", this.encryptedMessage); 

    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvalidAlgorithmParameterException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (BadPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

而且,我得到的例外是:

java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block 

回答

2

顯然你的IV必須是16個字節,而不是128字節。 (128位= 16字節)

+0

謝謝@Quanturium,你說得對,我應該用字節而不是位來表達它。將檢查它是否有效,並讓你知道。謝謝! – civiac 2013-03-13 11:40:06

+0

如果它解決了您的問題,您能否將此主題設置爲固定? – Quanturium 2013-03-16 00:40:35

+0

嗨@Quanturium,現在我得到一個新的例外。你可以好好看看嗎?我編輯了我的問題 – civiac 2013-03-26 22:07:06