@SuppressWarnings("UseSpecificCatch")
public static void encryptFile(String pass, File file, String input) throws BaseApplicationException {
if (pass == null) {
throw new BaseApplicationException("Null Password invalid.")
.setErrorType(BaseApplicationException.ErrorType.FATAL);
}
if (pass.length() == 0) {
throw new BaseApplicationException("Ivalid Password lenght.")
.setErrorType(BaseApplicationException.ErrorType.FATAL);
}
FileOutputStream fout = null;
ByteArrayInputStream bais = null;
CipherOutputStream out = null;
try {
char[] password = pass.toCharArray();
byte[] salt = salt();
SecretKeyFactory skf = SecretKeyFactory.getInstance(__SECRETKEY_MODE);
KeySpec ks = new PBEKeySpec(password, salt, ITER_SAVE_FILE, LEN_DEFAULT_CIP);
SecretKey tmp = skf.generateSecret(ks);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), __KEY_SPEC);
Cipher cipher = Cipher.getInstance(__CIP_MODE);
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters p = cipher.getParameters();
byte[] iv = p.getParameterSpec(IvParameterSpec.class).getIV();
fout = new FileOutputStream(file);
bais = new ByteArrayInputStream(Utility.getUTF8Bytes(input));
out = new CipherOutputStream(fout, cipher);
byte[] buffer = new byte[1024];
int n = 0;
fout.write(salt, 0, salt.length);
fout.flush();
fout.write(iv, 0, iv.length);
fout.flush();
while ((n = bais.read(buffer)) >= 0) {
out.write(buffer, 0, n);
out.flush();
}
} catch (Exception exc) {
System.err.println("Exception -encryptFile- exc:" + exc);
exc.printStackTrace();
throw new BaseApplicationException("Encrypt Error!").setException(exc);
} finally {
if (out != null) {
try {
out.flush();