Voilà j'ai un probleme de decryptage en AES. j'ai l'exception : "javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 16 bytes"
mon code :
et
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 public static SecretKey generateKeyAES128() throws NoSuchAlgorithmException { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(new String("la clef secret").getBytes()); keyGen.init(random); SecretKey key = keyGen.generateKey(); //keyGen.init(128); // return keyGen.generateKey(); return key; } public void initAES() throws SamiException { try { //Install SunJCE provider Provider sunJce = new SunJCE(); Security.addProvider(sunJce); // Create the key /*KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = kgen.generateKey();*/ SecretKey key= generateKeyAES128(); ecipher = Cipher.getInstance("AES"); dcipher = Cipher.getInstance("AES"); // Create the ciphers ecipher.init(Cipher.ENCRYPT_MODE, key); dcipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception e) { throw new SamiException("Echec d'instanciation de l'encryptage", EXC_FWK_SYSTEM, e); } }
Quelqu'un peut-il m'aider ?
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 String encrypt(String str) throws SamiException { try { // Encode the string into bytes using utf-8 byte[] utf8 = str.getBytes(); // Encrypt byte[] enc = ecipher.doFinal(utf8); // Encode bytes to base64 to get a string return new sun.misc.BASE64Encoder().encode(enc); } catch (Exception e) { throw new SamiException("Echec de l'encryptage", EXC_FWK_SYSTEM, e); } } public String decrypt(String str) throws SamiException { try { // Decode base64 to get bytes byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); // Decrypt byte[] utf8 = dcipher.doFinal(dec); // Decode using utf-8 return new String(utf8, "UTF8"); } catch (Exception e) { throw new SamiException("Echec du décryptage", EXC_FWK_SYSTEM, e); } }
Partager