2012-02-21 49 views
1

我試圖使用AES/CBC/PKCS5Padding做一個字符串的加密解密 我得到這個異常: javax.crypto.BadPaddingException:鑑於最終塊未正確填充AES/CBC/PKCS5Padding Java加密錯誤 - javax.crypto.BadPaddingException:給定最後的塊沒有正確填充

字符串我試圖加密:ftp.clarapoint.com

這裏是我的加密代碼:

cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");   
cipher.init(Cipher.ENCRYPT_MODE, aesKey); 
byte[] data = cipher.doFinal(stringDec.getBytes()); 
byte[] iv = cipher.getIV(); 

我傳送解密方法如下:aesKey,數據和iv

解密代碼:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
AlgorithmParameters.getInstance("AES"); 
cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv)); 
byte[] decrypted = cipher.doFinal(data); 

謝謝!

+0

已使用小訪問標籤,Rotem公司。我會添加加密... – 2012-02-23 22:09:53

+0

如果你跟進你的問題,Rotem會很好。 – 2012-02-27 02:13:59

回答

4

你是不是transfering無論是鍵或密文正確,因爲這個代碼並運行:

private static void testCode() { 
    try { 
     String stringDec = "Hi there"; 
     SecretKey aesKey = new SecretKeySpec(new byte[16], "AES"); 

     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
     cipher.init(Cipher.ENCRYPT_MODE, aesKey); 

     // no encoding given, don't use getBytes() without a Charset.forName("UTF-8") 
     byte[] data = cipher.doFinal(stringDec.getBytes()); 
     byte[] iv = cipher.getIV(); 

     // doesn't do anything 
     AlgorithmParameters.getInstance("AES"); 

     cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv)); 
     byte[] decrypted = cipher.doFinal(data); 
     System.out.println(new String(decrypted)); 
    } catch (GeneralSecurityException e) { 
     throw new IllegalStateException(e); 
    } 
} 
相關問題