@SuppressWarnings("UseSpecificCatch")
public static void encryptFile(@Nonnull String pass, @Nonnull File file, @Nonnull 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);
}
if (pass.length() > 16) {
pass = pass.substring(0, 16);
}
try {
pass = appendCPassData(pass);
} catch (Exception exc) {
throw new BaseApplicationException("Key Error!")
.setException(exc);
}
try {
byte[] password = Utility.getUTF8Bytes(pass);
ByteArrayInputStream secret
= new ByteArrayInputStream(Utility.getUTF8Bytes(input));
OutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
byte[] buff_of_iv_data = new byte[16];
SecureRandom sc_r = new SecureRandom();
sc_r.nextBytes(buff_of_iv_data);
out.write(buff_of_iv_data);
out.flush();
SecretKeySpec sks = new SecretKeySpec(password, __KEY_SPEC);
IvParameterSpec is = new IvParameterSpec(buff_of_iv_data);
Cipher c = Cipher.getInstance(__CIP_MODE);
c.init(Cipher.ENCRYPT_MODE, sks, is);
out = new CipherOutputStream(out, c);
@SuppressWarnings("UnusedAssignment")
int n = 0;
while ((n = secret.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
out.flush();
out.close();
} catch (Exception exc) {
Utility.log("Exception -encrypt- exc:" + exc);
Utility.exc(exc);
throw new BaseApplicationException("Encrypt Error!")
.setException(exc);
}
}