密码获取错误:: EVP_DecryptFinal_ex:解密错误

时间:2014-01-03 11:41:44

标签: java android encryption

我有以下代码用于解密我在Java中的数据,但它给了我“Bad Decrypt”错误。任何人都可以帮我吗?

     String input = "5H5h8acnv2gzv2PeTVb+pw==";
     String key = "thisismykey___2011_1234567898765";
        byte[] output = null;
        try{

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            output = cipher.doFinal(Base64.decode(input, Base64.DEFAULT));
            Log.i("word is: ", new String(output));
        }catch(Exception e){

            Log.d("myapp", "Error decrypting data", e);  // This is the line
        }

以下是我用来加密数据的PHP代码:

function getEncrypt($sStr, $sKey) {
    return base64_encode(
        mcrypt_encrypt(
            MCRYPT_RIJNDAEL_128, 
            $sKey,
            $sStr,
            MCRYPT_MODE_ECB
        )
    );
}

function getDecrypt($sStr, $sKey) {
    return mcrypt_decrypt(
        MCRYPT_RIJNDAEL_128, 
        $sKey, 
        base64_decode($sStr), 
        MCRYPT_MODE_ECB
    );
}
$key = "thisismykey___2011_1234567898765";
getEncrypt("Ekram",$key); // After encryption i get this "5H5h8acnv2gzv2PeTVb+pw=="

获得以下例外:

01-03 10:26:36.930: I/The Text is(911): error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

** * ** * *** 堆栈跟踪 ** * ** * ** * < / EM> ***

01-03 12:06:53.740: D/myapp(902): Error decrypting data
01-03 12:06:53.740: D/myapp(902): javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
01-03 12:06:53.740: D/myapp(902):   at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
01-03 12:06:53.740: D/myapp(902):   at com.android.org.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:420)
01-03 12:06:53.740: D/myapp(902):   at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:456)
01-03 12:06:53.740: D/myapp(902):   at javax.crypto.Cipher.doFinal(Cipher.java:1204)
01-03 12:06:53.740: D/myapp(902):   at com.example.android.fragments.MainActivity.EncryptDecryptTest(MainActivity.java:529)
01-03 12:06:53.740: D/myapp(902):   at com.example.android.fragments.MainActivity$5.onTouch(MainActivity.java:285)
01-03 12:06:53.740: D/myapp(902):   at android.view.View.dispatchTouchEvent(View.java:7701)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
01-03 12:06:53.740: D/myapp(902):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
01-03 12:06:53.740: D/myapp(902):   at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
01-03 12:06:53.740: D/myapp(902):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
01-03 12:06:53.740: D/myapp(902):   at android.view.View.dispatchPointerEvent(View.java:7886)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
01-03 12:06:53.740: D/myapp(902):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
01-03 12:06:53.740: D/myapp(902):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
01-03 12:06:53.740: D/myapp(902):   at android.os.MessageQueue.nativePollOnce(Native Method)
01-03 12:06:53.740: D/myapp(902):   at android.os.MessageQueue.next(MessageQueue.java:138)
01-03 12:06:53.740: D/myapp(902):   at android.os.Looper.loop(Looper.java:123)
01-03 12:06:53.740: D/myapp(902):   at android.app.ActivityThread.main(ActivityThread.java:5017)
01-03 12:06:53.740: D/myapp(902):   at java.lang.reflect.Method.invokeNative(Native Method)
01-03 12:06:53.740: D/myapp(902):   at java.lang.reflect.Method.invoke(Method.java:515)
01-03 12:06:53.740: D/myapp(902):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-03 12:06:53.740: D/myapp(902):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-03 12:06:53.740: D/myapp(902):   at dalvik.system.NativeStart.main(Native Method)
01-03 12:06:53.860: D/dalvikvm(902): GC_FOR_ALLOC freed 762K, 9% free 8545K/9372K, paused 32ms, total 36ms

1 个答案:

答案 0 :(得分:0)

尝试检查传递给cipher.doFinal()

的格式数据

我有同样的问题,但结果却是:

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

应改为

Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

并且异常消失了。我确实比较了如何在PHP中加密字符串并试图用Java解密它。