【问题标题】:Fast file encryption in androidandroid中的快速文件加密
【发布时间】:2017-05-23 10:04:29
【问题描述】:

您好,我正在创建一个示例来锁定 android 中的文件或文件夹。当我加密或解密大尺寸(超过 1 GB)文件时,我遇到了一个问题,它需要太多时间。 请帮我快速加密和解密文件。 这里我附上我在下面使用的代码

 if (!isEncrypted) {
                        FileInputStream fis = new FileInputStream(path);
                        // This stream write the encrypted text. This stream will be wrapped by another stream.
                        FileOutputStream fos = new FileOutputStream(path + ".abcd");

                        // Length is 16 byte
                        SecretKeySpec sks = new SecretKeySpec("abcdefghijklmnop".getBytes(), "AES");
                        // Create cipher
                        Cipher cipher = Cipher.getInstance("AES");
                        cipher.init(Cipher.ENCRYPT_MODE, sks);
                        // Wrap the output stream
                        CipherOutputStream cos = new CipherOutputStream(fos, cipher);
                        // Write bytes
                        int b;
                        byte[] d = new byte[1024];
                        try {
                            while ((b = fis.read(d)) != -1) {
                                cos.write(d, 0, b);
                            }

                            // Flush and close streams.
                            cos.flush();
                            cos.close();
                            fis.close();
                            new File(path).deleteOnExit();
                            isEncrypted = true;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    btnEncrypt.setText("Decrypt Path");
                                    deleteMyFile(path);
                                }
                            });
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    } else {
                        FileInputStream fis = new FileInputStream(path + ".abcd");

                        FileOutputStream fos = new FileOutputStream(path);
                        SecretKeySpec sks = new SecretKeySpec("abcdefghijklmnop".getBytes(), "AES");
                        Cipher cipher = Cipher.getInstance("AES");
                        cipher.init(Cipher.DECRYPT_MODE, sks);
                        CipherInputStream cis = new CipherInputStream(fis, cipher);
                        int b;
                        byte[] d = new byte[1024];


                        while ((b = cis.read(d)) != -1) {
                            fos.write(d, 0, b);
                        }
                        fos.flush();
                        fos.close();
                        cis.close();
                        isEncrypted = false;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                deleteMyFile(path + ".abcd");
                                btnEncrypt.setText("Encrypt Path");
                            }
                        });

                    }
                } catch (final Exception e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressDialog.setMessage(e.getMessage());
                        }
                    });

【问题讨论】:

  • “时间过长”有多长?此外,您使用的加密代码有许多弱点和不安全性。我不会在生产代码中使用它。
  • 感谢 Luke Park 阅读我的问题。需要2分钟,我想在一秒钟内完成。是否可以在一秒钟内锁定大文件?请帮忙
  • "是否可以在一秒钟内锁定大文件?" - 那要看。您正在构建自己的硬件吗?如果是这样,欢迎您尝试使用高端组件构建它以尝试达到该速度。在普通的 Android 硬件上,你不能在一秒钟内写入 1GB,忽略任何加密/解密。在某些硬件上,如果您能在一分钟内写入 1 GB,我会印象深刻。
  • 在我的 PC 上,我相信它有一个能够硬件加速 AES 的芯片,加密一个 ~1GB 文件需要 20.2 秒。普通手机(希望也不是高端手机!)远没有我的台式机那么快。这根本是不可能的。此外,您不是在“锁定”文件,而是在加密它。
  • 有什么方法可以在不加密文件夹文件的情况下锁定文件夹?我想锁定文件夹,我不想加密文件,但我无法锁定文件夹。

标签: android file encryption


【解决方案1】:

我希望你找到了一种方法来做到这一点。但是,这就是我的做法。

我意识到,如果您特别关注最终结果,那么不推荐使用 java 或 C 编写我们自己的 AES 类。与 C 相比,Java 相当慢,但在 java 中可以使用多线程,而在 C 中则不能。当然,有一些 aes c 程序做得更好。但是使用它们很困难。你说你想在一分钟内加密 1GB 的文件。所以,这样做吧。

安装 Userland 或 Termux 并安装 openssl。接下来 cd 到您的文件存储位置并尝试 aes encrypt 命令。要使其自动化,请编写一个 bash 脚本并运行它。 Openssl 可以轻松做到 23 MBps 到 68 MBps。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多