Java: getting rid of `Cipher.init ()` overhead

I need to increase the performance of the following method:

private byte[] decrypt(final byte[] encrypted, final Key key) throws ... {
    this.cipher.init(Cipher.DECRYPT_MODE, key);
    return this.cipher.doFinal(encrypted);
}

The object cipher("AES / ECB / NoPadding") is initialized in the constructor, so it can be reused. ECB is used because the array encryptedwill always contain only 16 bytes of data (i.e. 1 data block). A 128-bit key is used.

This method is called millions of times to decrypt a 16-byte chunk, each time with a different key. For instance. The method is called like this:

final List<Key> keys = List with millions of keys
final byte[] data = new byte[] { ... 16 bytes of data go here ...}

for (final Key key : keys) {
    final byte[] encrypted = decrypt(data, key);

    // Do something with encrypted
}

Cipher.init()takes up most of the time decryptsince the data is so small. That is, with more than 12 million calls, Cipher.init()on average it takes 3 microseconds, while Cipher.doFinal()0.5 microseconds on average.

  • What takes so long in Cipher.init()?
  • , Java? , , ?
  • C/++ JNI? , - ?

JDK 1.8.0_73, AES-NI.

+4
1

Cipher.init()?

, .

, Java? , , ?

, AES. JDK AESCrypt.java.

preinitialized Ciphers Keys.

C/++ JNI? , - ?

, . libcrypto, OpenSSL, . .

+6

All Articles