/*
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
*/
|
package com.megatim.fdxconvert.controller;
|
|
import com.megatim.module.encryption.impl.AESImpl;
|
import com.megatimfx.common.abstracts.AbstractEditDialogController;
|
import com.megatimfx.common.utils.ViewLoaderUtil;
|
import com.megatimfx.components.customdialogs.AlertMessageUtil;
|
import com.megatimfx.components.customdialogs.LoadinIndicatorDialogUtil;
|
import com.megatimfx.components.dialogs.NotificationDialog;
|
import com.megatimfx.components.dialogs.NotificationType;
|
import com.megatim.fdxconvert.forms.DecryptageAESEditFormController;
|
import com.megatim.fdxconvert.pojo.DecryptageAES;
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.URL;
|
import java.util.ResourceBundle;
|
import javafx.concurrent.Task;
|
import javafx.event.ActionEvent;
|
import javafx.scene.Node;
|
import javafx.scene.layout.Pane;
|
import javafx.stage.Stage;
|
|
/**
|
*
|
* @author STEPHANIE
|
*/
|
public class DecryptageAESEditDialogController extends AbstractEditDialogController<DecryptageAES> {
|
|
private DecryptageAESEditFormController decryptageAESEditFormController;
|
|
@Override
|
public void initialize(URL url, ResourceBundle rb) {
|
decryptageAESEditFormController = new DecryptageAESEditFormController();
|
super.initialize(url, rb);
|
}
|
|
@Override
|
public String getTitle() {
|
getEditButton().setText("Décrypter");
|
return "Décryptage d'un fichier";
|
}
|
|
@Override
|
public Pane getContentFormPane() throws IOException {
|
return ViewLoaderUtil.getPaneFromFxmlFile(decryptageAESEditFormController.getClass().getResource("DecryptageAESEditForm.fxml"), decryptageAESEditFormController);
|
}
|
|
@Override
|
public Object getContentFormController() {
|
return decryptageAESEditFormController;
|
}
|
|
@Override
|
public boolean beforeSave(ActionEvent event) {
|
boolean proceed = super.beforeSave(event);
|
boolean proceedKey = getCurrentObject().getKeyToDecrypt().equals(getCurrentObject().getKeyConfirmation());
|
proceed = proceed && proceedKey;
|
|
if (!proceedKey) {
|
com.megatimfx.common.customdialogs.LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
|
Node source = (Node) event.getSource();
|
Stage parentStage = (Stage) source.getScene().getWindow();
|
com.megatimfx.common.dialogs.NotificationDialog notificationDialog = new com.megatimfx.common.dialogs.NotificationDialog(
|
"La clé et sa confirmation doivent avoir la même valeur",
|
com.megatimfx.common.dialogs.NotificationType.ERROR,
|
parentStage
|
);
|
notificationDialog.showNotification();
|
}
|
return proceed;
|
}
|
|
@Override
|
public void afterSave(ActionEvent event) {
|
DecryptageAES dec = getCurrentObject();
|
Task<Void> task = new Task<Void>() {
|
@Override
|
protected Void call() throws Exception {
|
symetricDecrypt(dec);
|
return null;
|
}
|
};
|
|
task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
|
task.setOnFailed(e -> {
|
LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
|
Throwable th = task.getException();
|
Node source = (Node) event.getSource();
|
Stage parentStage = (Stage) source.getScene().getWindow();
|
|
if (th instanceof javax.crypto.BadPaddingException || th instanceof java.security.spec.InvalidKeySpecException
|
|| th instanceof java.security.InvalidKeyException) {
|
NotificationDialog notificationDialog = new NotificationDialog(
|
"Echec décryptage : La clé fournie n'est pas la bonne ",
|
NotificationType.ERROR,
|
parentStage
|
);
|
notificationDialog.showNotification();
|
|
} else if (th instanceof javax.crypto.IllegalBlockSizeException) {
|
NotificationDialog notificationDialog = new NotificationDialog(
|
"Echec décryptage : Ce fichier n'est pas crypté avec l'alogorithme AES ",
|
NotificationType.ERROR,
|
parentStage
|
);
|
notificationDialog.showNotification();
|
} else {
|
AlertMessageUtil.showAlertException(task.getException(), "Une exception s'est produite pendant le décryptage du fichier", "Erreur");
|
}
|
});
|
|
task.setOnSucceeded(e -> {
|
LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
|
Node source = (Node) event.getSource();
|
Stage parentStage = (Stage) source.getScene().getWindow();
|
NotificationDialog notificationDialog = new NotificationDialog(
|
"Décryptage du fichier réussie",
|
NotificationType.SUCCESS,
|
parentStage
|
);
|
notificationDialog.showNotification();
|
});
|
new Thread(task).start();
|
}
|
|
private void symetricDecrypt(DecryptageAES dec) throws Exception {
|
AESImpl aes = new AESImpl();
|
File inputFile = new File(dec.getFilePath());
|
File outputFile = new File(dec.getOutputDir(), inputFile.getName());
|
aes.decryptFile(dec.getKeyToDecrypt(), dec.getKeyLength(), dec.getFilePath(), outputFile.getAbsolutePath());
|
}
|
}
|