AesCtrJceCipher implements IndCpaCipher { @Override public byte[] decrypt(final byte[] ciphertext) throws GeneralSecurityException { if (ciphertext.length < ivSize) { throw new GeneralSecurityException("ciphertext too short"); } byte[] iv = new byte[ivSize]; System.arraycopy(ciphertext, 0, iv, 0, ivSize); byte[] plaintext = new byte[ciphertext.length - ivSize]; doCtr(ciphertext, ivSize, ciphertext.length - ivSize, plaintext, 0, iv, false); return plaintext; } AesCtrJceCipher(final byte[] key, int ivSize); @Override byte[] encrypt(final byte[] plaintext); @Override byte[] decrypt(final byte[] ciphertext);  }
@Test public void testNistVector() throws Exception { byte[] rawCiphertext = TestUtil.hexDecode(NIST_CIPHERTEXT); byte[] iv = TestUtil.hexDecode(NIST_IV); byte[] ciphertext = new byte[iv.length + rawCiphertext.length]; System.arraycopy(iv, 0, ciphertext, 0, iv.length); System.arraycopy(rawCiphertext, 0, ciphertext, iv.length, rawCiphertext.length); AesCtrJceCipher cipher = new AesCtrJceCipher(TestUtil.hexDecode(NIST_KEY), iv.length); assertArrayEquals(TestUtil.hexDecode(NIST_PLAINTEXT), cipher.decrypt(ciphertext)); }