【问题标题】:Android AES-128 encryption/decryption of file is very slow. How can I increase the speedAndroid AES-128 文件加密/解密速度非常慢。我怎样才能提高速度
【发布时间】:2015-01-18 21:47:33
【问题描述】:

我正在开发一个 android 应用程序来保护图像和视频,如 VaultyKeep safe。我正在尝试使用 AES-128 加密/解密技术来存储图像和视频。我通过分别拍摄 3 个大小为 5.13、4.76 和 5.31 的样本图像进行了尝试。但加密耗时分别为25s、22s、27s,解密耗时分别为31s、30s、34s。我正在 HTC One X 上测试它。

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

我试过VaultyKeep safe,它们都很快。据说 Vaulty 使用的是 AES-256,但它在加密和查看图像方面仍然非常快速且响应迅速。使用 AES-256 的 vaulty 怎么可能这么快?

我使用的代码是:

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

【问题讨论】:

    标签: android encryption cryptography aes bouncycastle


    【解决方案1】:

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

    byte[] d = new byte[8];
    

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

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

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

    【讨论】:

    • 非常感谢 @Mike 的帮助,我已将其设置为 10MB,即 byte[] d = new byte[10 * 1024 * 1024];加密:2.624s、3.61s、3.983s 解密:7.63、7.129、7.63 分别为 1.jpg、2.jpg 和 3.jpg 但对我来说解密速度较慢,应该更快。对于当前设备,我应该使用多少数组大小才不会导致应用程序崩溃?有没有办法找到最大缓冲区大小或最可行的缓冲区大小?
    • 我宁愿选择 8*1024 或 64*1024 之间的东西。非常大的缓冲区通常不会提高性能。
    • “但现在你可以将它实际设置为几 MB,即使在 Android 上也是如此”即使是“现在”,也不要在堆中分配 MB 的缓冲区 -> 正如 EJP 所写:几个 Kb 就足够了
    【解决方案2】:

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

    不需要像兆字节这样的巨大缓冲区大小:8k 或 32k 就足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2012-08-19
      • 2021-10-14
      • 1970-01-01
      • 2011-06-07
      相关资源
      最近更新 更多