public static void encryptFile(File originalFile, File encryptedFile, String password, String type) throws NoSuchAlgorithmException, NoSuchPaddingException {
YProgressWindowRepeat y = new YProgressWindowRepeat(I18N.t("Verschlüssle " + originalFile.getName()), "lock");
FileInputStream in = null;
CipherOutputStream out = null;
try {
// set basics
Cipher cipher = Cipher.getInstance(type);
SecretKey key = new SecretKeySpec(password.getBytes(), type);
cipher.init(Cipher.ENCRYPT_MODE, key);
// do it
in = new FileInputStream(originalFile);
out = new CipherOutputStream(new FileOutputStream(encryptedFile), cipher);
byte[] byteBuffer = new byte[1024];
for (int n; (n = in.read(byteBuffer)) != -1; out.write(byteBuffer, 0, n)) {
;
}
in.close();
out.close();
// new File(originalFile).delete();
} catch (Throwable t) {
YEx.warn("Can not encrypt File " + originalFile + " to " + encryptedFile, t);
} finally {
// close it
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
}
// close it
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
}
}