Bonjour à tous,

Je dois créer un programme permettant de crypter une chaîne de caractères en utilisant l'algorithme AES sur 256 bits.

J'ai trouvé un code source sur le site de Sun (pour un encodage sur 128 bits) mais je ne parviens pas à l'utiliser avec un encodage de 256 bits.

http://java.sun.com/developer/techni...ES/AES_v1.html

J'ai suivi les instructions de Sun en installant les bibliothèques : Strong Versus Unlimited Strength Cryptography, mais je ne suis pas sûr qu'elles soient bien prises en compte dans mon programme.

Malgré les 2 discussions déjà disponibles sur ce forum, je ne parviens pas à réaliser mon programme Java.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
 import java.security.*;
 import javax.crypto.*;
 import javax.crypto.spec.*;
 import java.io.*;
 
 /**
 * This program generates a AES key, retrieves its raw bytes, and
 * then reinstantiates a AES key from the key bytes.
 * The reinstantiated key is used to initialize a AES cipher for
 * encryption and decryption.
 */
 
 public class AES {
 
   /**
   * Turns array of bytes into string
   *
   * @param buf Array of bytes to convert to hex string
   * @return    Generated hex string
   */
   public static String asHex (byte buf[]) {
    StringBuffer strbuf = new StringBuffer(buf.length * 2);
    int i;
 
    for (i = 0; i < buf.length; i++) {
     if (((int) buf[i] & 0xff) < 0x10)
    strbuf.append("0");
 
     strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }
 
    return strbuf.toString();
   }
 
   public static void main(String[] args) throws Exception {
 
     String message="This is just an example";
 
     // Get the KeyGenerator
 
     //KeyGenerator kgen = KeyGenerator.getInstance("AES");
     //kgen.init(256); // 192 and 256 bits may not be available
 
 
     // Generate the secret key specs.
     	// SecretKey skey = kgen.generateKey();
     	// byte[] raw = skey.getEncoded();
 
			byte[] raw = "12365478965214211452442145145214".getBytes();
			SecretKey skeySpec = new SecretKeySpec(raw, "AES");
 
 
     // Instantiate the cipher
 
     Cipher cipher = Cipher.getInstance("AES");
 
     cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
 
     byte[] encrypted =
       cipher.doFinal((args.length == 0 ?
        "This is just an example" : args[0]).getBytes());
     System.out.println("encrypted string: " + asHex(encrypted));
 
     cipher.init(Cipher.DECRYPT_MODE, skeySpec);
     byte[] original =
       cipher.doFinal(encrypted);
     String originalString = new String(original);
     System.out.println("Original string: " +
       originalString + " " + asHex(original));
   }
 }
J'ai l'erreur suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters
        at javax.crypto.Cipher.a(DashoA12275)
        at javax.crypto.Cipher.a(DashoA12275)
        at javax.crypto.Cipher.a(DashoA12275)
        at javax.crypto.Cipher.init(DashoA12275)
        at javax.crypto.Cipher.init(DashoA12275)
        at AES.main(AES.java:57)
Merci d'avance pour votre aide.