@Test public void testMultipleEncrypts() throws Exception { byte[] key = Random.randBytes(16); int ivSize = 16; AesCtrJceCipher cipher = new AesCtrJceCipher(key, ivSize); byte[] c1 = cipher.encrypt(msg); byte[] c2 = cipher.encrypt(msg); assertEquals(c1.length, c2.length); assertFalse(Arrays.equals(c1, c2)); }
@Override public byte[] encrypt(final byte[] plaintext) throws GeneralSecurityException { if (plaintext.length > Integer.MAX_VALUE - ivSize) { throw new GeneralSecurityException( "plaintext length can not exceed " + (Integer.MAX_VALUE - ivSize)); } byte[] ciphertext = new byte[ivSize + plaintext.length]; byte[] iv = Random.randBytes(ivSize); System.arraycopy(iv, 0, ciphertext, 0, ivSize); doCtr(plaintext, 0, plaintext.length, ciphertext, ivSize, iv, true); return ciphertext; }
AesCtrJceCipher implements IndCpaCipher { @Override public byte[] encrypt(final byte[] plaintext) throws GeneralSecurityException { if (plaintext.length > Integer.MAX_VALUE - ivSize) { throw new GeneralSecurityException( "plaintext length can not exceed " + (Integer.MAX_VALUE - ivSize)); } byte[] ciphertext = new byte[ivSize + plaintext.length]; byte[] iv = Random.randBytes(ivSize); System.arraycopy(iv, 0, ciphertext, 0, ivSize); doCtr(plaintext, 0, plaintext.length, ciphertext, ivSize, iv, true); return ciphertext; } }
AesCtrJceCipher implements IndCpaCipher { @Override public byte[] encrypt(final byte[] plaintext) throws GeneralSecurityException { if (plaintext.length > Integer.MAX_VALUE - ivSize) { throw new GeneralSecurityException( "plaintext length can not exceed " + (Integer.MAX_VALUE - ivSize)); } byte[] ciphertext = new byte[ivSize + plaintext.length]; byte[] iv = Random.randBytes(ivSize); System.arraycopy(iv, 0, ciphertext, 0, ivSize); doCtr(plaintext, 0, plaintext.length, ciphertext, ivSize, iv, true); return ciphertext; } AesCtrJceCipher(final byte[] key, int ivSize); }
AesCtrJceCipher implements IndCpaCipher { @Override public byte[] encrypt(final byte[] plaintext) throws GeneralSecurityException { if (plaintext.length > Integer.MAX_VALUE - ivSize) { throw new GeneralSecurityException( "plaintext length can not exceed " + (Integer.MAX_VALUE - ivSize)); } byte[] ciphertext = new byte[ivSize + plaintext.length]; byte[] iv = Random.randBytes(ivSize); System.arraycopy(iv, 0, ciphertext, 0, ivSize); doCtr(plaintext, 0, plaintext.length, ciphertext, ivSize, iv, true); return ciphertext; } AesCtrJceCipher(final byte[] key, int ivSize); @Override byte[] encrypt(final byte[] plaintext); @Override byte[] decrypt(final byte[] ciphertext); }
AesCtrJceCipher implements IndCpaCipher { @Override public byte[] encrypt(final byte[] plaintext) throws GeneralSecurityException { if (plaintext.length > Integer.MAX_VALUE - ivSize) { throw new GeneralSecurityException( "plaintext length can not exceed " + (Integer.MAX_VALUE - ivSize)); } byte[] ciphertext = new byte[ivSize + plaintext.length]; byte[] iv = Random.randBytes(ivSize); System.arraycopy(iv, 0, ciphertext, 0, ivSize); doCtr(plaintext, 0, plaintext.length, ciphertext, ivSize, iv, true); return ciphertext; } AesCtrJceCipher(final byte[] key, int ivSize); @Override byte[] encrypt(final byte[] plaintext); @Override byte[] decrypt(final byte[] ciphertext);  }