Bonjour,

J'ai écrit le code suivant qui permet de chiffrer et déchiffrer un message :
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
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
 
public class CipherAESTest {
 
    public static void main(String[] args) {
 
        try {
            byte[] salt = "salt".getBytes();
            char[] password = {'k', 'h', 'a', 'l', 'e', 'd'};
            PBEKeySpec pbe = new PBEKeySpec(password, salt, 30, 256);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHAAND256BITAES-CBC-BC", "BC");
            SecretKey generatedKey = skf.generateSecret(pbe);
            IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
 
            String message = "On a deux vies. La deuxième commence le jour où on réalise qu'on en a juste une";
            Cipher encCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            encCipher.init(Cipher.ENCRYPT_MODE, generatedKey, iv);
            byte[] enc = encCipher.doFinal(message.getBytes());
            System.out.println("encrypted text : " + new String(enc));
 
            Cipher decCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            decCipher.init(Cipher.DECRYPT_MODE, generatedKey, iv);
            byte[] dec = decCipher.doFinal(enc);
            System.out.println("decrypted text : " + new String(dec));
 
        } catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException |
                InvalidKeyException | InvalidAlgorithmParameterException | InvalidKeySpecException |
                NoSuchAlgorithmException | NoSuchProviderException ex) {
            ex.printStackTrace();
        }
    }
}
Lors de l’exécution, je n’obtiens pas la chaine originale, quelqu'un saurait-il m'indiquer où est l'erreur ?

D'avance merci.