2

I am trying to decrypt a voice file after encrypting it, uploading it to Firebase then downloading it. and for the encryption/decryption I am using EasyCrypt

  • The encrypting and uploading done successfully.
  • The downloading is done successfully.
  • I have checked the encrypting key and it is the same as the decrypting one.
  • The permissions are granted.
    • The decryption works on the same device even after downloading the new file.

When I get the downloaded file on another device and try to decrypt it like:

final File decryptedVoice = new File(myFilePath + "/" + dateHolder + ".mp4");
final File encryptedFile = new File(voiceURL);
progressBar.setVisibility(View.VISIBLE);
ECSymmetric ecSymmetric = new ECSymmetric();
ecSymmetric.decrypt(encryptedFile, voiceKey, new ECResultListener() {
    @Override
    public void onProgress(int i, long l, long l1) {
        Log.e("EncryptedFile", encryptedFile.getPath());
        Log.e("DecryptedFile", decryptedVoice.getAbsolutePath());
        Log.e(" DecryptionKey", decryptionKey);
        Log.i("VoiceDecryption", String.valueOf((l*100)/l1));
    }

    @Override
    public <T> void onSuccess(T t) {
        progressBar.setVisibility(View.GONE);
        pause.setVisibility(View.VISIBLE);
        try {
            mediaPlayer.setDataSource(decryptedVoice.getAbsolutePath());
            mediaPlayer.prepare();
            mediaPlayer.setVolume(10,10);
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    seekBar.setMax(mediaPlayer.getDuration());
                    mediaPlayer.start();
                    update(mediaPlayer, time, seekBar);


                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(String s, Exception e) {
        Log.e(s, e.toString());
    }
}, decryptedVoice);

The decryption is done 99% then I get the following error:

E/Cannot write to file.: java.io.IOException: Error while finalizing cipher

Here is the full stack of the error:

08-18 18:54:30.578 30039-30989/com.berbangchat.me I/VoiceDecryption: 99
08-18 18:54:30.583 30039-30989/com.berbangchat.me W/System.err: java.io.IOException: Error while finalizing cipher
        at javax.crypto.CipherInputStream.fillBuffer(CipherInputStream.java:104)
        at javax.crypto.CipherInputStream.read(CipherInputStream.java:155)
        at java.io.InputStream.read(InputStream.java:162)
        at com.pvryan.easycrypt.symmetric.performDecrypt.invoke$easycrypt_release(performDecrypt.kt:124)
        at com.pvryan.easycrypt.symmetric.ECSymmetric$decrypt$1.invoke(ECSymmetric.kt:209)
        at com.pvryan.easycrypt.symmetric.ECSymmetric$decrypt$1.invoke(ECSymmetric.kt:44)
        at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Async.kt:140)
        at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Async.kt)
        at org.jetbrains.anko.AsyncKt$sam$Callable$761a5578.call(Async.kt)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
    Caused by: javax.crypto.BadPaddingException: error:1e06b065:Cipher functions:EVP_DecryptFinal_ex:BAD_DECRYPT
08-18 18:54:30.583 30039-30989/com.berbangchat.me W/System.err:     at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
08-18 18:54:30.583 30039-30989/com.berbangchat.me W/System.err:     at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:568)
        at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:385)
        at javax.crypto.Cipher.doFinal(Cipher.java:1476)
08-18 18:54:30.583 30039-30989/com.berbangchat.me W/System.err:     at javax.crypto.CipherInputStream.fillBuffer(CipherInputStream.java:102)
        ... 14 more
08-18 18:54:30.583 30039-30989/com.berbangchat.me E/CipherError: Cannot write to file. : java.io.IOException: Error while finalizing cipher

Note: The tested devices are not running the same android versions, one is Marshmallow and one is Oreo.

  • 1
    Is EasyCrypt any good? How do you know it's even supported? – President James K. Polk Aug 24 '18 at 17:56
  • 1
    I didn't get it @JamesKPolk but I need this library because it has onSuccess callback because I want to run a function just when the encryption is done not before it. it makes a difference when the files are in MBs. Do you recommend any other solutions? –  Aug 24 '18 at 18:31
  • 1
    No, but you don't want to have to debug both your code *and* some rarely used, unheard of library. I don't really know if EasyCrypt is rarely used and unheard of, but the documentation looks suspiciously poor and it seems to support AES-CTR mode without a MAC; both are bad signs. – President James K. Polk Aug 24 '18 at 18:56
  • 1
    How are you sending the encrypted data from one device to another? Are you formatting or compressing it before sending it to firebase? Can you post that code? – J. Jefferson Aug 27 '18 at 14:36
  • 1
    @J.Jefferson I do that through uploading and downloading from Firebase Storage. without any compression or edits after the encryption is done. I do that using the usual ways that are listed in Firebase docs. https://firebase.google.com/docs/storage/web/upload-files & https://firebase.google.com/docs/storage/web/download-files –  Aug 27 '18 at 17:03

1 Answers1

2

I believe your issue is that devices aren’t running the same OpenSSL versions used for encryption and decryption. I searched a different error in your error log.

Caused by: javax.crypto.BadPaddingException: error:1e06b065:Cipher functions:EVP_DecryptFinal_ex:BAD_DECRYPT

Check out this SO question for more information. I know this doesn’t answer your question fully, but I hope it gets you started.

J. Jefferson
  • 980
  • 8
  • 12
  • Sounds like the real reason but can you suggest any solution for this case? –  Aug 26 '18 at 22:45