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

在eclipse中,當我選擇KeyGenerator並右鍵單擊打開聲明時,我打開一個窗口。開放聲明中的這個錯誤是什麼?

click here to see error image(圖片鏈接斷開)

你能解釋一下什麼是錯在這裏嗎?順便說一句這裏是完整的代碼

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

回答

1

這只是顯示出源到KeyGenerator類沒有隨JDK - 想必這會違反出口限制或類似的東西。 (我的Eclipse安裝中也收到了相同的消息,儘管其他類顯示源代碼沒有問題,所以我懷疑它不是配置問題。)

您是否真的需要查看源代碼到KeyGenerator?你想知道什麼?

+0

源代碼不存在,但那裏是執行keygenerator類的類文件/二進制文件嗎? – rover12 2009-11-19 10:24:02

+0

換句話說.. ..我的程序運行那部分代碼或不是? – rover12 2009-11-19 10:24:37

+1

是的,二進制代碼在那裏,而不是源。如果二進制文件不存在,則會導致編譯時失敗。 – 2009-11-19 10:27:23