Am using the standard technique of using a RSA key/pair whose public key encrypts a 16 byte random key which encrypts my data using AES/CBC/PKCS5Padding.
I am using bouncy castle for my needs I need to encrypt streams of usually large data (512MB+).
On running performance tests to understand the overhead of encryption I am seeing that encryption is nearly 30-40% more expensive than un-encrypted data.
Is this expected ?
Sample code
public InputStream encryptStream(InputStream streamToEncrypt, byte[] key, byte[] iv, byte[] encryptedKey // 256 bytes) {
final Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, key, iv);
byte[] civ = cipher.getIV();
...
ByteArrayInputStream ivEncryptedKeyStream = new ByteArrayInputStream(ivEncryptedKeyArray);
CipherInputStream encrypted = new CipherInputStream(streamToEncrypt, cipher);
return new SequenceInputStream(ivEncryptedKeyStream, encrypted);
}
elsewhere
InputStream encryptedStream = ...encryptStream(plainStream, key, iv, encKey);
IOUtils.copyLarge(encryptedStream, outputStream);
I have played around with java server args ; confirmed that the AES-NI instruction set is on etc. Just wanted to have an idea on what overhead should I be expecting with encrypting large streams ?
EDIT : Corrected information that I am using bouncycastle just for the key-pair generation. For AES Encryption using SunJCE as the security provider.