0

I'm developing an application that encrypts its input files.
My Code is this for encrypting files (File size differs from some KB to 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();

Can I make it more optimum (Time,IO,CPU)?
How?

Thanks

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Ariyan
  • 14,760
  • 31
  • 112
  • 175

3 Answers3

2

This is a complicated and difficult question to answer. First of all, encrypting 4GB worth of file is going to take time no matter what you do.

A lightweight encryption algorithm like the Hummingbird will help you get there - but, you have to make sure that wherever you're using this, AES isn't absolutely necessary.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
1

Picking a better byte[] block size will help (something in the 8k to 1MB range, test to find the optimium size for your scenario), but other than that there's not much you can do to make it faster (assuming you maintain the current encryption algorithm).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • What algorithm do you suggest for faster encryption? – Ariyan Aug 31 '12 at 20:13
  • @4r1y4n - i'm not really a crypto expert in that regards. i'm sure there is [plenty of info online](http://www.cse.wustl.edu/~jain/cse567-06/ftp/encryption_perf/index.html), though. – jtahlborn Aug 31 '12 at 21:06
1

What you can do is to you use AESFastEngine from bouncycastle library to accelerate aes block computation.

crypto/src/org/bouncycastle/crypto/engines

This fast engine uses T tables which are rounded precomputed table and for sure will have a gain in performance.

Littm
  • 4,923
  • 4
  • 30
  • 38
FdL
  • 11
  • 1