Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow AES decryption in Android

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 {
                SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                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);
                os.write(decrypted);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            long end = System.currentTimeMillis()/1000L;

            android.util.Log.d("TEST","Time "+ String.valueOf(end-start));
like image 871
Lorensius W. L. T Avatar asked Jan 11 '11 23:01

Lorensius W. L. T


People also ask

How long does AES decryption take?

With the right quantum computer, AES-128 would take about 2.61*10^12 years to crack, while AES-256 would take 2.29*10^32 years. For reference, the universe is currently about 1.38×10^10 years old, so cracking AES-128 with a quantum computer would take about 200 times longer than the universe has existed.

How long does it take to encrypt AES?

Today, a powerful machine can crack a 56-bit DES key in 362 seconds. On the other hand, cracking a 128-bit AES encryption key can take up to 36 quadrillion years.

Which is faster AES encryption or AES decryption?

AES is a symmetric encryption, and is the same speed whether encrypting or decrypting.

Is AES encryption slow?

AES itself is very fast. Your 'over a 1000 hash iterations' suggests you are using a Password-Based encryption scheme, which is different from plain AES.


1 Answers

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).

like image 51
Thomas Pornin Avatar answered Oct 04 '22 07:10

Thomas Pornin