Bonjour,
N'ayant pas trouvé de solution sur le forum, je vous expose mon problème :
Pour des raisons de sécurité, je dois crypter des données avant de les stocker dans une bdd sql. Ensuite, je dois pouvoir afficher les données recherchées de ma base dans une application en décrypté.
J'arrive donc à stocker mes données cryptées dans ma base sans problème mais je n'arrive pas à les décrypter par la suite. Voici l'erreur générée:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
javax.crypto.BadPaddingException: Given final block not properly padded
Voici maintenant le code utilisé pour crypter et 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
	public static String encrypt (String cookieValue) throws InvalidKeyException, UnsupportedEncodingException {
	    SecurityAES aes = new SecurityAES();
	    aes.generateKey();
	    byte[] ciphertext = aes.crypt(cookieValue);   
	    BASE64Encoder encoder = new BASE64Encoder();
	    String encryptedString = encoder.encode(ciphertext);
	    return encryptedString;
	}
 
	public static String decrypt (String cookieValue) throws InvalidKeyException, IOException {
		SecurityAES aes = new SecurityAES();
	    aes.generateKey();
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] encryptedValue = decoder.decodeBuffer(cookieValue);
		return aes.decryptInString(encryptedValue);
	}
et les fonctions appelées:
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
public final static int KEY_SIZE = 128; 
	private SecretKeySpec secretKeySpec;
 
	public SecurityAES() {
	}
 
	public void generateKey() {
		try {
			KeyGenerator keyGen = KeyGenerator.getInstance("AES");
			keyGen.init(KEY_SIZE);
			SecretKey secretKey = keyGen.generateKey();  
			byte[] raw = secretKey.getEncoded();
			secretKeySpec = new SecretKeySpec(raw, "AES");
		}
		catch (Exception e) {
			System.out.println(e);
		} 
	}
 
	public byte[] crypt(byte[] plaintext) {
		try {
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
			return cipher.doFinal(plaintext);    
		}
		catch (Exception e) {
			System.out.println(e);
		} 
		return null;
	}
 
	public byte[] crypt(String plaintext) throws UnsupportedEncodingException{
	    return crypt(plaintext.getBytes());
	}
 
	public byte[] decryptInBytes(byte[] ciphertext) {
		try {
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
			return cipher.doFinal(ciphertext);
		}
		catch (Exception e) {
			System.out.println(e);
		} 
		return null;
	}
 
	public String decryptInString(byte[] ciphertext) {
		return new String(decryptInBytes(ciphertext));
	}
Merci d'avance pour vos réponses !