Kenmegne
6 days ago 908e81dd173f380879d5fabeb5794d5b7a76df0e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * 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());
    }
}