3

I'm trying to encrypt a 512 Mb file with AES/CBC algorithm. It is taking around 7 sec which is too much. How to reduce the encryption time and make it faster.

I'm using a fixed key and tried using CipherOutStream as well as cipher.update() instead of cipher.dofinal(). Still, it is taking around 7 secs.

How much time generally it would take to encrypt a 512 MB file with the below encryption. It is taking 6 seconds for me on a Mac with 16 GB memory and 2 GHz Quad-Core Intel Core i5 processor. I'm using JDK 11 for execution. Is it normal or my code is responding slow. Should I be concerned? How to improve the time for encryption.

package com.encrypterdecrypter;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

@SpringBootApplication
public class DemoApplication {
    private static final String SECRET_KEY = "aesencryptionKey";
    private static final String initVector = "encryptionIntVec";

    public static void main(String[] args) throws Exception {
        SpringApplication.run(DemoApplication.class, args);
        File inputFile = new ClassPathResource("fivetwelvemb.zip").getFile();
        InputStream inputStream = new FileInputStream(inputFile);
        SecretKeySpec secretkey = new SecretKeySpec(SECRET_KEY.getBytes("UTF-8"), "AES");


        long startTime = System.currentTimeMillis();
        OutputStream encryptStream = encryptDecryptBinary(inputStream, Cipher.ENCRYPT_MODE, secretkey);
        long endTime = System.currentTimeMillis();
        System.out.println("Encryption Time in ms : " + (endTime - startTime));

    }

    public static OutputStream encryptDecryptBinary(InputStream inputStream, int encryptMode, SecretKeySpec secretkey) throws Exception {
        Cipher aesCipher = Cipher.getInstance("AES/CBC/NoPadding");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        aesCipher.init(encryptMode, secretkey);
        OutputStream out =  new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(out, aesCipher);

       /* byte[] encryptedData = aesCipher.doFinal(inputStream.readAllBytes()); */

        byte[] buf = new byte[8192];
        int numRead = 0;
        while ((numRead = inputStream.read(buf)) >= 0) {
            cipherOutputStream.write(buf, 0, numRead);
        }
        cipherOutputStream.close();
        return out;
    }
}
```
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 2
    how long does it take to process the file using these methods if you comment out the cryptography and just write to a new file ? – Richie Frame Sep 18 '20 at 00:26
  • 1
    If I just transfer Inputstream to Outputstream, it is taking around 800ms. – Seelam Venkatanagasiva Sep 18 '20 at 00:44
  • Java tends to be slow until it completes its warm up cycle. Perhaps you could try using an external crypto library (many people use Bouncy castle). – Serpent27 Sep 18 '20 at 01:51
  • I just updated my CPU, OS, and environment in the question. @fgrieu – Seelam Venkatanagasiva Sep 18 '20 at 15:03
  • 2 GHz Quad-Core Intel Core i5 Will this be any help or any other information about the processor needed. – Seelam Venkatanagasiva Sep 18 '20 at 20:19
  • I suspect it's very much about if the implementation of AES used by the JVM is Java, native, or native with [AES-NI](https://en.wikipedia.org/wiki/AES_instruction_set#Intel) (which dramatically boosts AES). Core i5 is a marketing name, it does not tell if there's AES-NI. It's not in the [core i5 2415M](https://ark.intel.com/content/www/us/en/ark/products/53449/intel-core-i5-2415m-processor-3m-cache-up-to-2-90-ghz.html) that was [used in some Macs](https://everymac.com/systems/apple/mac_mini/specs/mac-mini-core-i5-2.3-mid-2011-specs.html). Beside I found no Mac exactly matching your spec. – fgrieu Sep 19 '20 at 07:05
  • if you transfer input to output, are you still doing it in buffer sizes of 8192 bytes? – Richie Frame Sep 20 '20 at 02:32
  • Yes. I'm using the same 8192 bytes size – Seelam Venkatanagasiva Sep 24 '20 at 21:43

0 Answers0