Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cipher getting error :: EVP_DecryptFinal_ex:bad decrypt

I have following code for decrypting my data in Java but it gives me "Bad Decrypt" error. Can anyone help me on this?

     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
        }

Here is the PHP code I used to encrypt the data:

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=="

Getting following Exception:

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

********* Stack Trace ************

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
like image 651
Ekram Avatar asked Jan 03 '14 11:01

Ekram


1 Answers

Try to check what format data you are passing to cipher.doFinal()

I had same issue but turns out line:

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

should be changed to

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

and the exception gone. I did compare how strings were being encrypted in PHP and was trying to decrypt it in Java.

like image 171
Andrew Danylyuk Avatar answered Sep 28 '22 08:09

Andrew Danylyuk