SecretManager { public String encrypt(final String str) { if (str == null) { throw new NullPointerException("Argument cannot be null"); } try { final SecureRandom random = new SecureRandom(); final byte[] salt = new byte[16]; random.nextBytes(salt); final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); final KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt, 65536, 128); final SecretKey tmp = factory.generateSecret(spec); final SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); final AlgorithmParameters params = cipher.getParameters(); final byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); final byte[] encryptedText = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(salt); outputStream.write(iv); outputStream.write(encryptedText); return DatatypeConverter.printBase64Binary(outputStream.toByteArray()); } catch (final Exception exception) { throw new RuntimeException(exception.getMessage(), exception); } } SecretManager(final String passphrase); String encrypt(final String str); String decrypt(final String str);  }
@Test public void doTestWithNullInput() { final SecretManager secretManager = new SecretManager("key"); expectedException.expect(NullPointerException.class); final String encrypted = secretManager.encrypt(null); }