Take the 2-minute tour ×

I tried to decrypt a 4.2 MB .dcf file using AES 128 bit key, but it took 33 seconds to decrypt (on function cipher.doFinal(data)), is it normal ?

Here is a code snippet:

long start = System.currentTimeMillis()/1000L;
            try {
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);

                 android.util.Log.d("TEST", "Start decoding...." + String.valueOf(length));

                byte[] decrypted = cipher.doFinal(content);

                File file2 = new File(Environment.getExternalStorageDirectory().getPath() + "/test.mp3");
                OutputStream os = new FileOutputStream(file2);
            } catch (Exception ex) {
            long end = System.currentTimeMillis()/1000L;

            android.util.Log.d("TEST","Time "+ String.valueOf(end-start));
share|improve this question
I expect this will depend on the hardware - my Desire does certain things a lot quicker than my wife's Wildfire, for example. What are you trying this on? –  Squonk Jan 12 '11 at 0:09
@MisterSquonk, I tried it on emulator, it took about 30-33 secs, on my Samsung Galaxy Spica, it took about 25-30 secs. –  Lorensius W. L. T Jan 12 '11 at 15:33
If you explain how I'd create such a file as the one you're decrypting, I'll try your code on my HTC Desire (Froyo) if it would be of any use to you to know the results. –  Squonk Jan 13 '11 at 0:29
@MisterSquonk: I've tried on my friend's N1, it took only 2s. You're right, it depends on hardware and froyo (with jit compiler) –  Lorensius W. L. T Jan 15 '11 at 17:58
Wow, that's quite a big difference. Interesting to know, thanks for the update. –  Squonk Jan 16 '11 at 0:58
add comment

1 Answer

up vote 2 down vote accepted

You should try to bench the time taken without the file writing, i.e. call System.currentTimeMillis() right before and right after the call to cipher.doFinal().

That being said, an Android-based phone typically uses a recent ARM processor clocked at 500 MHz or more, and such a beast is theoretically able to AES-encrypt or AES-decrypt several megabytes worth of data per second.

However, Android code uses an almost-Java virtual machine called Dalvik. Prior to Android-2.2, this is an interpreter (no JIT compiler), which means that it is kinda slow for computing-intensive tasks. If the mediocre performance you observe really comes from the AES operation itself (and not the file writing) then the plausible answer is that your VM provides an AES implementation that is written in Java and interpreted with Dalvik. In that case, there is little cure except hoping for the presence of a better VM implementation (a VM could use a native code implementation for AES; also, with Android 2.2 and later, Dalvik has a JIT compiler which should boost performance of code execution).

share|improve this answer
Hi Thomas, thanx for your answer. I have removed the file writing and it took about 30-33 seconds on cipher.doFinal function on froyo emulator or 25-30 secs on Samsung Galaxy Spica (eclair). My friend did the the same task on Blackberry and it took about 8 seconds. –  Lorensius W. L. T Jan 12 '11 at 15:48
add comment

Your Answer


