慢AES解密的Andr​​oid [英] Slow AES decryption in Android

查看:133
本文介绍了慢AES解密的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解密使用AES 128位密钥的4.2 MB .dcf中的文件,但花了33秒解密(在功能cipher.doFinal(数据)),是正常的吗?

下面是一个code片断:

 长开始= System.currentTimeMillis的()/ 1000L;
            尝试 {
                SecretKeySpec skeySpec =新SecretKeySpec(键,AES);
                密密码= Cipher.getInstance(AES / CBC / PKCS5Padding);
                cipher.init(Cipher.DECRYPT_MODE,skeySpec,ivspec);

                 android.util.Log.d(测试,开始解码....+将String.valueOf(长度));

                byte []的解密= cipher.doFinal(内容);

                文件文件2 =新的文件(Environment.getExternalStorageDirectory()getPath()+/test.mp3。);
                的OutputStream OS =新的FileOutputStream(文件2);
                os.write(解密);
            }赶上(例外前){
                ex.printStackTrace();
            }
            长端= System.currentTimeMillis的()/ 1000L;

            android.util.Log.d(TEST,时间+将String.valueOf(年底启动));
 

解决方案

您应该尝试板凳没有文件写入所需的时间,也就是呼叫 System.currentTimeMillis的()右侧前后调用之后 cipher.doFinal()

话虽这么说,基于Android的手机通常使用最近的ARM处理器,主频为500 MHz或更高,这​​样的兽理论上能够AES-加密或AES-解密几兆值得每秒的数据。

然而的,Android的code使用一个几乎Java虚拟机被称为的的Dalvik 。之前Android的2.2,这是一个跨preTER(无JIT编译器),这意味着其是用于计算密集型任务有点慢。 如果的业绩平平,你看到真正来自于AES操作本身(而不是文件写入)的然后的似是而非的答案是,你的虚拟机提供的是写在一个AES实现Java和PTED与Dalvik的跨$ P $。在这种情况下,有除了希望更好的虚拟机实现了presence小固化(虚拟机可以使用本机code实现的AES;同时,与Android 2.2及更高版本,Dalvik的有一个的 JIT编译器应提高code的执行性能)。

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

解决方案

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

这篇关于慢AES解密的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆