建议Java中的文件加密性能

ket*_*tan 6 java encryption performance

我正在做一些与文件加密相关的工作.我能够加密/解密文件,但面临一个主要的性能问题.当我只读取/写入700 MB大小的视频文件时,我的代码执行大约27-28 MB/s.但是当我执行加密时(我目前正在使用PBEWithMD5AndDES,我将在稍后更改)代码显示速度为9 MB/s.请告知我在哪里可以改进.

代码段:

    int c = 0, BUF_SIZE = 8192;
    byte[] b = new byte[BUF_SIZE];
    FileInputStream fis;
    DataInputStream dis;
    FileOutputStream fos;
    DataOutputStream dos;
    CipherOutputStream cos;


    try {
        // Create PBE parameter set
        pbeParamSpec = new PBEParameterSpec(salt, iterationCount);

        // Create PBE Cipher
        Cipher pbeCipher = Cipher.getInstance(algorithm);

        // get key
        key = generateKeyFromPassword(password);

        // Initialize PBE Cipher with key and parameters
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);

        fis = new FileInputStream(inFile);
        dis = new DataInputStream(fis);
        fos = new FileOutputStream(outFile);
        dos = new DataOutputStream(fos);
        cos = new CipherOutputStream(fos, pbeCipher);


        while ((c = dis.read(b)) > 0) {
            cos.write(b);
            //dos.write(b);
        }

        fis.close();
        dis.close();
        //dos.close();
        cos.close();


    } catch (Exception e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

没有加密的统计数据:
速度大约是27.97 MB/s
确切时间= 25.02秒
文件大小= 700 MB

加密统计:
速度约为9.69 MB/s
确切时间= 72.171秒
文件大小= 700 MB

小智 0

加密只是 CPU 密集型的。也许您可以找到更高效的实现方式并减少运行时间,或者使用专用的硬件引擎并以相当多的美元获得更好的性能。

我要确保的第一件事是您的应用程序能够应对这需要一段时间的事实。这意味着将加密货币放入后台操作,在使用之前准备内容以及类似的设计考虑。