2011-10-22 103 views
1

我想在java中使用PKCS5Padding編寫AES 256 CBC加密的加密/解密方法。我目前正試圖解密來自另一個Base64編碼源的加密文本。下面javax.crypto.BadPaddingException AES 256 CBC加密

代碼: (這只是測試數據,所以它不是敏感)

// JUnit Test 
    @Test 
    public void testDecrypt() { 
     String cipherText = "rrAwZQCAIj19XauZE6tQEg/HQuWB7gw+1uVO0hylyWyCSJo/y7uB6Xj4BRVi+a3qY9GQ/ahjPdUF/kSHptt6QttkvQf89JS13Mo3mRAnaDK/8uoRur8TDuKzLtCSjaMAg72LqObx04+yLd9hI2krtCaWd2saCLP/cWvTQ9oc1xQ="; 
     String iv = "o1clHzdEkUV4sFj72VwDFQ=="; 
     String syncKey = "gbh7teqqcgyzd65svjgibd7tqy"; 

     SecretKeySpec key = new SecretKeySpec(convertFromBase32(syncKey), "AES"); 
     byte[] cipherBytes = convertFromBase64(cipherText); 
     System.out.println(cipherBytes.length); 
     Encrypted d = Crypto.decrypt(new Encrypted(cipherBytes, key, 
       convertFromBase64(iv))); 
     String decryptedText = new String(d.getCipherText()); 
    } 

// Actual Code 
public static Encrypted decrypt(Encrypted encrypted) { 
     // Initialize the Cipher 
     Cipher cipher = null; 
     IvParameterSpec ivParam = new IvParameterSpec(
       encrypted.getInitializationVector()); 
     try { 
      cipher = Cipher.getInstance(TRANSFORMATION); 
      cipher.init(Cipher.DECRYPT_MODE, encrypted.getSymmetricKey(), 
        ivParam); 
     } catch (NoSuchAlgorithmException e1) { 
      e1.printStackTrace(); 
     } catch (NoSuchPaddingException e1) { 
      e1.printStackTrace(); 
     } catch (InvalidKeyException e) { 
      e.printStackTrace(); 
     } catch (InvalidAlgorithmParameterException e) { 
      e.printStackTrace(); 
     } 

     byte[] outputBytes = cryptCommon(cipher, encrypted.getCipherText()); 
     Encrypted decrypted = new Encrypted(outputBytes, 
       encrypted.getSymmetricKey(), cipher.getIV()); 
     return decrypted; 
    } 

    private static byte[] cryptCommon(Cipher cipher, byte[] inputBytes) { 
     byte[] outputBytes = null; 
     try { 
      outputBytes = cipher.doFinal(inputBytes); 
     } catch (IllegalBlockSizeException e) { 
      e.printStackTrace(); 
     } catch (BadPaddingException e) { 
      e.printStackTrace(); 
     } 
     return outputBytes; 
    } 

的我已經檢查字節[]的長度從Base64的解碼後和它們長度的整除按塊大小(對於16字節塊大小,爲128字節)。

這裏是堆棧跟蹤我越來越:

javax.crypto.BadPaddingException: Given final block not properly padded 
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) 
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) 
    at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) 
    at javax.crypto.Cipher.doFinal(DashoA13*..) 
    at com.mozilla.android.sync.Crypto.cryptCommon(Crypto.java:77) 
    at com.mozilla.android.sync.Crypto.decrypt(Crypto.java:69) 
    at com.mozilla.android.sync.test.CryptoTests.testDecrypt(CryptoTests.java:71) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

任何幫助深表感謝。謝謝!

+0

'TRANSFORMATION'的價值是什麼? –

+0

對不起,變形=「AES/CBC/PKCS5Padding」 – jvoll

回答

0

在密碼學中,一切都必須準確無誤。你是否將加密和解密方法都設置爲PKCS5填充?你是否檢查過加密方法產生的字節與解密函數消耗的字節是否完全一致?

你檢查過兩個鍵是否完全匹配,字節是字節嗎?您在一點使用Base32,這有點不尋常。

我建議將測試劃分爲更小的部分,這樣您可以明確檢查每個參數的匹配情況,以進行加密和解密。確保你檢查每個參數爲一個字節數組,即不涉及從Base64或其他任何的轉換。

+0

是的,這個錯誤最終與你在這裏所說的類似。糟糕! – jvoll