Java: Get rid of `Cipher.init()` overhead
<p>I need to increase performance of the following method:</p>

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

<p>The <code>cipher</code> object ("AES/ECB/NoPadding") is initialized in the constructor, so that it can be reused. ECB is used since the <code>encrypted</code> array will contain always only 16 bytes of data (i.e. 1 block of data). 128 bit key is used.</p>

<p>This method is called millions of times to decrypt 16 byte chunk, each time with a different key. E.g. the method is called like this:</p>

<pre><code>final List&lt;Key&gt; 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
}
</code></pre>

<p>The <code>Cipher.init()</code> takes most of the time of the <code>decrypt</code> method, since the data is so small. I.e. with over 12 million invocations, <code>Cipher.init()</code> takes 3 microseconds on average while <code>Cipher.doFinal()</code> takes &lt; 0.5 microseconds on average.</p>

<ul>
<li>What takes so long in <code>Cipher.init()</code>?</li>
<li>Is there any way to speed up this code using Java only? For instance by taking advantage of the fact I will be decrypting always only a single block of data?</li>
<li>Would it be faster to use a C/C++ implementation and call it using JNI? If so, is there any well-established library for that?</li>
</ul>

<p>I use JDK 1.8.0_73 and my processor supports AES-NI.</p>