2009-11-19 3 views
0
KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
    kgen.init(128); // 192 and 256 bits may not be available 

En éclipse, lorsque je sélectionne KeyGenerator et que je clique avec le bouton droit de la déclaration, une fenêtre s'ouvre.Quelle est cette erreur sur la déclaration ouverte?

click here to see error image (lien cassé image)

Pouvez-vous expliquer ce qui est erroné ici? BTW voici le code complet

package org.temp2.cod1; 

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 AES1 
{ 

    /** 
    * 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(128); // 192 and 256 bits may not be available 


     // Generate the secret key specs. 
     SecretKey skey = kgen.generateKey(); 
     byte[] raw = skey.getEncoded(); 

     SecretKeySpec 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)); 
    } 
} 

Répondre

1

Il est juste montrer que la source à la classe KeyGenerator n'est pas livré avec le JDK - on peut supposer qu'il serait contraire à restrictions à l'exportation ou quelque chose de similaire. (Je reçois le même message sur mon installation Eclipse, malgré d'autres classes affichant la source sans problèmes, donc je suppose que ce n'est pas un problème de configuration.)

Avez-vous vraiment besoin de voir la source à KeyGenerator? Qu'essayais-tu de découvrir?

+0

le code source n'est pas là, mais sont les fichiers de classe/fichiers binaires là pour exécuter la classe keygenerator? – rover12

+0

en d'autres termes .. mon programme exécute-t-il cette partie du code ou pas? – rover12

+1

Oui, le code binaire est là, mais pas la source. Si les fichiers binaires n'étaient pas présents, vous obtiendrez un échec lors de la compilation. –

Questions connexes