Hello tout le monde ! J'ai un pti soucis alors je viens vers vous
j'ai créé une classe qui récupère une clé qui se trouve dans un fichier, et j'ai implémenté deux méthodes permettant de crypter et décrypter un texte...
Voici ces méthodes :
Dans une autre classe qui doit crypter et décrypter un texte, j'ai écrit :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /** cryptage d'un texte passé en paramètre **/ public static String cryptage(String s) { String txtNouveauPswd = s; byte[] plainText = txtNouveauPswd.getBytes(); // transforme la chaine en bytes et stocke dans un tableau loadKey(); try { // Création de l'objet cipher DES Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // Encryptage en utilisant la clé et plainText //System.out.println("\nDébut d'encryptage"); cipher.init(Cipher.ENCRYPT_MODE, key); cipherText = cipher.doFinal(plainText); //System.out.println("Fin d'encryptage : "); //System.out.println(new String(cipherText, "UTF8")); pswdCrypte = new String(cipherText, "UTF8"); } catch(Exception exc) { exc.printStackTrace(); System.out.println("\n " + exc.getMessage() + "\n "); } return pswdCrypte; } /** décryptage d'un texte crypté **/ public static String decryptage(String ss) { loadKey(); try { // Création de l'objet cipher DES Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); byte[] plainText = ss.getBytes(); // Décryption du cipher en utilisant la clé générée lors du cryptage //System.out.println("\nDébut de decryptage : "); cipher.init(Cipher.DECRYPT_MODE, key); byte[] newPlainText = cipher.doFinal(plainText); //System.out.println("Fin de decryptage : "); pswdDecrypte = new String(newPlainText, "UTF8"); //System.out.println(new String(newPlainText, "UTF8")); } catch(IllegalBlockSizeException i) { } catch(Exception excp) { excp.printStackTrace(); System.out.println("\n " + excp.getMessage() + "\n "); } return pswdDecrypte; }
Le résultat : le cryptage marche bien, j'ai fait un test qui affiche le texte crypté... cependant le décryptage ne marche pas, et "null" est renvoyé sur la console...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 if (e.getSource() == btnValider) { try { String p = new String(txtNouveauPswd1.getText()); System.out.println(clCrypt.cryptage(p)); System.out.println(clCrypt.decryptage(clCrypt.cryptage(p))); //crypterPswd(); //decrypterPswd(cipherText, key); } catch(Exception r) { } }
Si quelqu'un pouvait m'aider ce seait génial !
Merci d'avance![]()
Partager