@Test public void doTestWithNullDecrypt() { final SecretManager secretManager = new SecretManager("key"); assertNull("Should have null result", secretManager.decrypt(null)); }
public String decrypt(final String str) { if (str == null || str.isEmpty()) { return str; } try { final byte[] ciphertext = DatatypeConverter.parseBase64Binary(str); if (ciphertext.length < 48) { return null; } final byte[] salt = Arrays.copyOfRange(ciphertext, 0, 16); final byte[] iv = Arrays.copyOfRange(ciphertext, 16, 32); final byte[] ct = Arrays.copyOfRange(ciphertext, 32, ciphertext.length); 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.DECRYPT_MODE, secret, new IvParameterSpec(iv)); final byte[] plaintext = cipher.doFinal(ct); return new String(plaintext, StandardCharsets.UTF_8); } catch (final Exception exception) { throw new RuntimeException(exception.getMessage(), exception); } }
SecretManager { public String decrypt(final String str) { if (str == null || str.isEmpty()) { return str; } try { final byte[] ciphertext = DatatypeConverter.parseBase64Binary(str); if (ciphertext.length < 48) { return null; } final byte[] salt = Arrays.copyOfRange(ciphertext, 0, 16); final byte[] iv = Arrays.copyOfRange(ciphertext, 16, 32); final byte[] ct = Arrays.copyOfRange(ciphertext, 32, ciphertext.length); 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.DECRYPT_MODE, secret, new IvParameterSpec(iv)); final byte[] plaintext = cipher.doFinal(ct); return new String(plaintext, StandardCharsets.UTF_8); } catch (final Exception exception) { throw new RuntimeException(exception.getMessage(), exception); } } }
SecretManager { public String decrypt(final String str) { if (str == null || str.isEmpty()) { return str; } try { final byte[] ciphertext = DatatypeConverter.parseBase64Binary(str); if (ciphertext.length < 48) { return null; } final byte[] salt = Arrays.copyOfRange(ciphertext, 0, 16); final byte[] iv = Arrays.copyOfRange(ciphertext, 16, 32); final byte[] ct = Arrays.copyOfRange(ciphertext, 32, ciphertext.length); 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.DECRYPT_MODE, secret, new IvParameterSpec(iv)); final byte[] plaintext = cipher.doFinal(ct); return new String(plaintext, StandardCharsets.UTF_8); } catch (final Exception exception) { throw new RuntimeException(exception.getMessage(), exception); } } SecretManager(final String passphrase); }
SecretManager { public String decrypt(final String str) { if (str == null || str.isEmpty()) { return str; } try { final byte[] ciphertext = DatatypeConverter.parseBase64Binary(str); if (ciphertext.length < 48) { return null; } final byte[] salt = Arrays.copyOfRange(ciphertext, 0, 16); final byte[] iv = Arrays.copyOfRange(ciphertext, 16, 32); final byte[] ct = Arrays.copyOfRange(ciphertext, 32, ciphertext.length); 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.DECRYPT_MODE, secret, new IvParameterSpec(iv)); final byte[] plaintext = cipher.doFinal(ct); return new String(plaintext, StandardCharsets.UTF_8); } catch (final Exception exception) { throw new RuntimeException(exception.getMessage(), exception); } } SecretManager(final String passphrase); String encrypt(final String str); String decrypt(final String str); }
SecretManager { public String decrypt(final String str) { if (str == null || str.isEmpty()) { return str; } try { final byte[] ciphertext = DatatypeConverter.parseBase64Binary(str); if (ciphertext.length < 48) { return null; } final byte[] salt = Arrays.copyOfRange(ciphertext, 0, 16); final byte[] iv = Arrays.copyOfRange(ciphertext, 16, 32); final byte[] ct = Arrays.copyOfRange(ciphertext, 32, ciphertext.length); 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.DECRYPT_MODE, secret, new IvParameterSpec(iv)); final byte[] plaintext = cipher.doFinal(ct); return new String(plaintext, StandardCharsets.UTF_8); } catch (final Exception exception) { throw new RuntimeException(exception.getMessage(), exception); } } SecretManager(final String passphrase); String encrypt(final String str); String decrypt(final String str);  }