java - 优化 Java 中的文件加密

标签 java optimization io bouncycastle jce

我正在开发一个加密其输入文件的应用程序。
我的代码是用于加密文件的(文件大小从 KB 到 4GB 不等):

    SecretKeySpec   key = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    Cipher          cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
    byte[] block = new byte[8];
    int i;

    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

    BufferedInputStream bIn=new BufferedInputStream(new ProgressMonitorInputStream(null,"Encrypting ...",new FileInputStream("input")));
    CipherInputStream       cIn = new CipherInputStream(bIn, cipher);
    BufferedOutputStream bOut=new BufferedOutputStream(new FileOutputStream("output.enc"));

    int ch;
    while ((i = cIn.read(block)) != -1) {
        bOut.write(block, 0, i);
    }
    cIn.close();
    bOut.close();

我可以让它更优化(时间、IO、CPU)吗?
怎么办?

谢谢

最佳答案

这是一个复杂且难以回答的问题。首先,无论您做什么,加密 4GB 的文件都需要时间。

Hummingbird这样的轻量级加密算法将帮助您实现这一目标 - 但是,您必须确保无论您在何处使用它,AES 都不是绝对必要的。

关于java - 优化 Java 中的文件加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221483/

相关文章:

java - Android 中 String.join 的替代方案?

java - 验证 2 个字母数字的正则表达式

mysql - 使用 Using temporary 解释 mysql performance 中的计划;使用文件排序;使用索引条件

c++ - 0x50E2DF58 处的未处理异常 (msvcp120d.dll)

JAVA - 从文件读取并写入另一个文件

java - Linux 关机命令

java - 索引越界异常 Java 索引 : 3, 大小:3

algorithm - 增加一组数字,使 XOR 和为 0

c++ - 运行缓慢的方向变换代码

C程序将空格分隔的输入字符串转换为int数组