2011-10-22 23 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パディングに暗号化と復号化の両方の方法を設定しましたか?暗号化メソッドによって生成されたバイトが、復号化関数によって消費されたバイトと正確に一致することを確認しましたか?

2つのキーが完全に一致していることを確認しましたか?あなたは一度にBase32を使用していますが、それは少し珍しいことです。

あなたのテストをはるかに小さなものに分割することをお勧めします。そのため、暗号化と復号化の両方について、すべてのパラメータの一致を明示的にチェックできます。各パラメータをバイト配列としてチェックすることを忘れないでください。つまり、Base64などからの変換は含まれません。

+0

ええ、間違いはあなたがここで言ったものに似ていました。 Ooops。 – jvoll

関連する問題