Android AES-128文件加密/解密非常慢。如何提高速度 [英] Android AES-128 encryption/decryption of file is very slow. How can I increase the speed

查看:443
本文介绍了Android AES-128文件加密/解密非常慢。如何提高速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用,该应用可以保护 Vaulty 保持安全。我正在尝试使用AES-128加密/解密技术来存储图像和视频。我尝试通过分别拍摄3个大小分别为5.13、4.76和5.31的样本图像来进行尝试。但是加密消耗的时间分别为25s,22s,27s,解密时间分别为31s,30s,34s。我正在HTC One X上进行测试。

I am developing an android app that secures images and videos like Vaulty and Keep safe. I am trying to use AES-128 encryption/decryption technique to store images and videos. I tried it by taking 3 sample images of size 5.13, 4.76 and 5.31 respectively. But the time it is consuming to encrypt is 25s, 22s, 27s respectively and time to decrypt is 31s, 30s, 34s respectively. I am testing it on HTC One X.

这种速度对于我的应用程序将是不可行的,因为用户可以快速滚动和查看图像而不会受到干扰。您能否建议我如何提高性能(速度),还是应该切换到其他算法?您能建议我其他方法来快速加密/解密图像和视频,而又不会过多损害安全性吗?

Such speed wont be feasible for my app as users will scroll and view images quickly without interruption. Can you please suggest me how can I improve the performance(speed) or should i switch to other algorithms? Can you please suggest me any other techniques through which i can encrypt/decrypt images and videos quickly without compromising security too much.

我尝试了 Vaulty 保持安全,它们很快。据说Vaulty使用的是AES-256,但它在加密和查看图像方面仍然非常快且响应迅速。

I tried Vaulty and Keep safe, and they are very quick. Vaulty is said to be using AES-256, but it is still very fast and responsive in terms of encrypting and viewing images. How is it possible that vaulty is that quick using AES-256?

我正在使用的代码是:

 static void encrypt(String filename) throws IOException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException {

    // Here you read the cleartext.
    File extStore = Environment.getExternalStorageDirectory();
    startTime = System.currentTimeMillis();
    Log.i("Encryption Started",extStore + "/5mbtest/"+filename);
    FileInputStream fis = new FileInputStream(extStore + "/5mbtest/"+filename);
    // This stream write the encrypted text. This stream will be wrapped by
    // another stream.



    FileOutputStream fos = new FileOutputStream(extStore + "/5mbtest/"+filename+".aes", false);

    // Length is 16 byte
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
            "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
    stopTime = System.currentTimeMillis();
    Log.i("Encryption Ended",extStore + "/5mbtest/"+filename+".aes");
    Log.i("Time Elapsed", ((stopTime - startTime)/1000.0)+"");
}

static void decrypt(String filename) throws IOException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException {
    File extStore = Environment.getExternalStorageDirectory();
    Log.i("Decryption Started",extStore + "/5mbtest/"+filename+".aes");
    FileInputStream fis = new FileInputStream(extStore + "/5mbtest/"+filename+".aes");

    FileOutputStream fos = new FileOutputStream(extStore + "/5mbtest/"+"decrypted"+filename,false);
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
            "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    startTime = System.currentTimeMillis();
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    stopTime = System.currentTimeMillis();

    Log.i("Decryption Ended",extStore + "/5mbtest/"+"decrypted"+filename);
    Log.i("Time Elapsed", ((stopTime - startTime)/1000.0)+"");

    fos.flush();
    fos.close();
    cis.close();
}


推荐答案

一件事使您代码运行缓慢是缓冲区的大小:

One thing that is making your code run slow is the size of your buffer:

byte[] d = new byte[8];

如果要使其快速运行,则应将其提高几个数量级。考虑到文件的大小,我建议至少使用1 MB,但如今,即使在Android平台上,您也可以将其设置为几MB。尝试将其更改为:

You should bump it up by a few orders of magnitude if you want it to run fast. Given the size of your files I would suggest using at least 1 MB but nowadays you can realistically set it to a few MBs, even on Android. Try changing it to:

byte[] d = new byte[1024 * 1024];

,然后让我们知道这样做可以提高速度。

and let us know how much did that improve the speed by.

这篇关于Android AES-128文件加密/解密非常慢。如何提高速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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