PasswordUtils { private String decrypt() throws IOException { String ret = null; try { byte[] decodedPassword = Base64.decode(password); Cipher engine = Cipher.getInstance(cryptAlgo); PBEKeySpec keySpec = new PBEKeySpec(encryptKey); SecretKeyFactory skf = SecretKeyFactory.getInstance(cryptAlgo); SecretKey key = skf.generateSecret(keySpec); engine.init(Cipher.DECRYPT_MODE, key,new PBEParameterSpec(salt, iterationCount, new IvParameterSpec(iv))); String decrypted = new String(engine.doFinal(decodedPassword)); int foundAt = decrypted.indexOf(LEN_SEPARATOR_STR); if (foundAt > -1) { if (decrypted.length() > foundAt) { ret = decrypted.substring(foundAt+1); } else { ret = ""; } } else { ret = null; } } catch(Throwable t) { LOG.error("Unable to decrypt password due to error", t); throw new IOException("Unable to decrypt password due to error", t); } return ret; } PasswordUtils(String aPassword); static String encryptPassword(String aPassword); static PasswordUtils build(String aPassword); static String decryptPassword(String aPassword); static boolean needsIv(String cryptoAlgo); static String generateIvIfNeeded(String cryptAlgo); String getCryptAlgo(); String getPassword(); int getIterationCount(); char[] getEncryptKey(); byte[] getSalt(); byte[] getIv(); String getIvAsString(); static String getDecryptPassword(String password); static final String PBE_SHA512_AES_128; static final String DEFAULT_CRYPT_ALGO; static final String DEFAULT_ENCRYPT_KEY; static final String DEFAULT_SALT; static final int DEFAULT_ITERATION_COUNT; static final byte[] DEFAULT_INITIAL_VECTOR; }
@Test public void testDecrypt() throws IOException { String decryptedPassword = PasswordUtils .decryptPassword("ljoJ3gf4T018Xr+BujPAqBDW8Onp1PqprsLKmxus8pGGBETtAVU6OQ=="); assertNotNull(decryptedPassword); assertEquals("secretPasswordNoOneWillEverKnow", decryptedPassword); }