salut tout le monde
svp j'ai besoin de votre aide :
jai trouve sur ce site un code source qui implémente le DES le voila :
le problème que je trouve des problèmes lors modification
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 public class PrivateExample { public static void main(String[] args) throws Exception { // // check args and get plaintext if (args.length != 1) { System.err.println("Usage: java PrivateExample text"); System.exit(1); } // byte[] plainText = args[0].getBytes("UTF8"); String ss = "Hello world, haris is here!"; byte[] plainText = ss.getBytes(); // // get a DES private key System.out.println("\nStart generating DES key"); KeyGenerator keyGen = KeyGenerator.getInstance("DES"); keyGen.init(56); Key key = keyGen.generateKey(); System.out.println("Finish generating DES key"); // // get a DES cipher object and print the provider Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); System.out.println("\n" + cipher.getProvider().getInfo()); // // encrypt using the key and the plaintext System.out.println("\nStart encryption"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = cipher.doFinal(plainText); System.out.println("Finish encryption: "); System.out.println(new String(cipherText, "UTF8")); // // decrypt the ciphertext using the same key System.out.println("\nStart decryption"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] newPlainText = cipher.doFinal(cipherText); System.out.println("Finish decryption: "); System.out.println(new String(newPlainText, "UTF8")); } }
je souhait modifier la dernière partie qui s'occupe de décryptage pour qui 'elle décrypte a partir de la chaine de caractère crypte et non pas a partir de l'objet cipherText initialise dans la partie de cryptage
voila ce que j'ai fait :
merci de votre aide
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 public class PrivateExample { public static void main(String[] args) throws Exception { String chaineACrypte = "Hello world, haris is here!"; // get a DES private key KeyGenerator keyGen = KeyGenerator.getInstance("DES"); keyGen.init(56); Key key = keyGen.generateKey(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = cipher.doFinal(chaineACrypte.getBytes()); String chaineCrypte =new String(cipherText, "UTF8"); //comment je peux utiliser la chaineCrypte et la key pour touver la chaine initiale ? cipher.init(Cipher.DECRYPT_MODE, key); byte[] newPlainText = cipher.doFinal(chaineCrypte.getBytes()); //erreur : Input length must be multiple of 8 when decrypting with padded cipher System.out.println(new String(newPlainText, "UTF8")); } }
Partager