Android AES-128加密/解密文件非常慢。我怎样才能提高速度

时间:2021-05-12 18:32:35

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.

我正在开发一个Android应用程序,可以保护像Vaulty和Keep安全的图像和视频。我正在尝试使用AES-128加密/解密技术来存储图像和视频。我通过分别拍摄尺寸为5.13,4.76和5.31的3个样本图像来尝试它。但加密消耗的时间分别为25秒,22秒,27秒,解密时间分别为31秒,30秒,34秒。我在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.

这样的速度对我的应用程序来说不可行,因为用户可以快速滚动和查看图像而不会中断。你能否建议我如何提高性能(速度)或者我应该切换到其他算法?你可以建议我使用任何其他技术来快速加密/解密图像和视频,而不会过多地影响安全性。

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?

我尝试了Vaulty并保持安全,而且它们非常快。据说Vaulty使用的是AES-256,但在加密和查看图像方面仍然非常快速且反应灵敏。 vaulty如何快速使用AES-256?

Code I am using is:

我正在使用的代码是:

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

2 个解决方案

#1


8  

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

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

byte[] d = new byte[8];

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:

如果你想让它快速运行,你应该将它提高几个数量级。鉴于您的文件大小,我建议使用至少1 MB,但现在您可以实际将其设置为几MB,即使在Android上也是如此。尝试将其更改为:

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

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

让我们知道提高速度有多少。

#2


5  

Use a larger buffer as suggested by @MikeLaren, and also wrap the FileOutputStream in a BufferedOutputStream. When decrypting, wrap the FileInputStream in a BufferedInputStream. Or do both in both cases: no harm done.

使用@MikeLaren建议的更大缓冲区,并将FileOutputStream包装在BufferedOutputStream中。解密时,将FileInputStream包装在BufferedInputStream中。或者两种情况都做到:没有伤害。

No need for heroic buffer sizes like a megabyte: 8k or 32k is sufficient.

不需要英雄缓冲区大小,如兆字节:8k或32k就足够了。

#1


8  

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

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

byte[] d = new byte[8];

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:

如果你想让它快速运行,你应该将它提高几个数量级。鉴于您的文件大小,我建议使用至少1 MB,但现在您可以实际将其设置为几MB,即使在Android上也是如此。尝试将其更改为:

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

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

让我们知道提高速度有多少。

#2


5  

Use a larger buffer as suggested by @MikeLaren, and also wrap the FileOutputStream in a BufferedOutputStream. When decrypting, wrap the FileInputStream in a BufferedInputStream. Or do both in both cases: no harm done.

使用@MikeLaren建议的更大缓冲区,并将FileOutputStream包装在BufferedOutputStream中。解密时,将FileInputStream包装在BufferedInputStream中。或者两种情况都做到:没有伤害。

No need for heroic buffer sizes like a megabyte: 8k or 32k is sufficient.

不需要英雄缓冲区大小,如兆字节:8k或32k就足够了。