Bonjour,
Je dois développer deux fonctions permettant de crypter et de décrypter avec en paramètre un string et une clef.
Pour cet excercice j'ai choisi d'utiliser le cryptage Blowfish, j'ai bien réussi a crypter mais lors du décryptage une erreure survient et je ne la comprend pas:
"Input length must be multiple of 8 when decrypting with padded cipher"
Mes fonctions :
Fonction pour crypter:
Fonction pour décrypter:
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 public String encrypt() throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // création secret key + mdp String password = "bouboul"; String key = "toto"; // génère tableau de byte byte[] keydata = key.getBytes(); // encryption de la clef SecretKeySpec KS = new SecretKeySpec(keydata, "Blowfish"); // creation cipher try { Cipher cipher = Cipher.getInstance("Blowfish"); // initialisation du cryptage cipher // Passe en @param mode et clef cipher.init(Cipher.ENCRYPT_MODE, KS); this.mdp = new String(cipher.doFinal(password.getBytes())); System.out.println(this.mdp); } catch (NoSuchAlgorithmException e) { System.err.println(e); } catch (NoSuchPaddingException e) { System.err.println(e); } return this.mdp; }
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 public String decrypt(String password) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException { String key = "toto"; byte[] keydata = key.getBytes(); SecretKeySpec KS = new SecretKeySpec(keydata, "Blowfish"); try { Cipher cipher = Cipher.getInstance("Blowfish"); cipher.init(Cipher.DECRYPT_MODE, KS); password = new String(cipher.doFinal(this.mdp.getBytes())); System.out.println(password); } catch (NoSuchAlgorithmException e) { System.err.println(e); } catch (NoSuchPaddingException e) { System.err.println(e); } return password; }
Vous remarquerez que je n'ai aps mis en paramètre le string et la clef car je les ai mis en dur pour les Tests merci d'avance !!!
Partager