2014-02-10 92 views
0

嗨,我有一個加密字符串來獲取用戶的pdf的問題。它顯示了一個錯誤,以下:加密的字符串長度

javax.crypto.IllegalBlockSizeException:帶襯墊的密

代碼解密時輸入長度必須是多個8是有:

public void getPDF(WebRequest request, ResourceResponse response, Model model, 
        @RequestParam("cif") String cif, 
        @RequestParam("cuenta") String cuenta, 
        @RequestParam("objectId") String encryptedObjectId) throws Exception{ 

    log.info("Inicio metodo getPDF"); 

    OutputStream os = response.getPortletOutputStream(); 


    try { 

     CipherHelper cipher = new CipherHelper(CipherHelper.TRIPLE_DES_ALGORITHM, InterfazConstantes.ENCRYPTION_KEY, InterfazConstantes.ENCRYPTION_SHIFT); 
     String objectId = cipher.decrypt(encryptedObjectId, true); 

public String getEncryptedObjectID() { 
    try { 
     CipherHelper cipher = new CipherHelper(CipherHelper.TRIPLE_DES_ALGORITHM, InterfazConstantes.ENCRYPTION_KEY, InterfazConstantes.ENCRYPTION_SHIFT); 
     encryptedObjectID = cipher.encrypt(objectID, true); 
    } catch (Exception e) { 
    } 

    return encryptedObjectID; 
} 

**我建立了encryptedObject ID有40個長度,但它沒有解決問題jet。

錯誤的軌跡是:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher 
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) ~[sunjce_provider.jar:1.6] 
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) ~[sunjce_provider.jar:1.6] 

有人能幫助我嗎?

感謝

+1

不知道'CipherHelper'幹什麼,這將不可能在這裏幫助。請將您的代碼作爲一個[最小示例](http://stackoverflow.com/help/mcve)來演示您的問題。 –

+0

這顯示總是這個錯誤,雖然我的字符串encryptedObjectId有8,16,32或64長度。這有些困惑,我認爲這個問題可能與這個參數的加密/解密有關。 –

+0

我已投票結束。沒有更完整的代碼示例,我們無法提供幫助。 –

回答

0

的問題是很可能是由於使用的字符串爲二進制數據(加密的文本= byte[])。 然後使用默認編碼。

String s = new String(bytes, StandardCharsets.ISO_8859_1);// Single byte encoding 
byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1); 
new OutputStreamWriter(outputStream, StandardCharsets.ISO_8859_1) 
new InputStreamReader(inputStream, StandardCharsets.ISO_8859_1) 
+0

我應該將一個byte []傳遞給cipher.encrypt嗎?謝謝 –

+0

取決於API。我不知道類CipherHelper,它可能是,使用字節的十六進制表示,在這種情況下,我的答案是沒有意義的,但「加密」通常應該接收文本(字符串)併產生二進制數據('byte []')和「解密」相反。 –