public static byte[] decrypt(PBEAlgo algo, byte[] cipherTextWithIv, char[] password,
int iterationCount, byte[] salt) throws GeneralSecurityException {
Args.notNull(cipherTextWithIv, "cipherTextWithIv");
Args.notNull(password, "password");
Args.positive(iterationCount, "iterationCount");
Args.notNull(salt, "salt");
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algo.algoName());
SecretKey pbeKey = secretKeyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(algo.algoName());
byte bb = cipherTextWithIv[0];
int ivLen = (bb < 0) ? 256 + bb : bb;
PBEParameterSpec pbeParameterSpec;
if (ivLen == 0) {
pbeParameterSpec = new PBEParameterSpec(salt, iterationCount);
} else {
byte[] iv = new byte[ivLen];
System.arraycopy(cipherTextWithIv, 1, iv, 0, ivLen);
pbeParameterSpec = new PBEParameterSpec(salt, iterationCount, new IvParameterSpec(iv));
}
int cipherTextOffset = 1 + ivLen;
byte[] cipherText = new byte[cipherTextWithIv.length - cipherTextOffset];
System.arraycopy(cipherTextWithIv, 1 + ivLen, cipherText, 0, cipherText.length);
cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParameterSpec);
return cipher.doFinal(cipherText);
}