EncryptionService { public byte[] encrypt(String plaintext) throws EncryptionServiceException { try { byte[] newSalt = generateRandomArray(PBKDF2_SALT_SIZE_BYTES); SecretKey key = new SecretKeySpec(generateKey(newSalt), CIPHER); Cipher myCipher = Cipher.getInstance(CIPHERSCHEME); byte[] newNonce = generateRandomArray(GCM_IV_NONCE_SIZE_BYTES); GCMParameterSpec spec = new GCMParameterSpec(GCM_AUTHENTICATION_TAG_SIZE_BITS, newNonce); myCipher.init(Cipher.ENCRYPT_MODE, key, spec); byte[] bytes = plaintext.getBytes(); return Arrays.concatenate(newNonce, newSalt, myCipher.doFinal(bytes)); } catch (Exception e) { logger.error("Encryption failed", e); throw new EncryptionServiceException(e); } } EncryptionService(String passphrase); byte[] encrypt(String plaintext); byte[] decrypt(byte[] encrypt);  }
@Test public void encrypt_shouldReturnDifferentCiphertextEachTime() throws EncryptionServiceException { byte[] ciphertext1 = service.encrypt("bob"); byte[] ciphertext2 = service.encrypt("bob"); assertThat(ciphertext1, not(ciphertext2)); }