2013-03-01 25 views
5

我得到java.security.InvalidKeyException:非法的密鑰大小或默認參數,我已經休整了所有必需的步驟,安裝Java加密擴展(JCE)無限強度管轄權限策略文件。 此外,我已經通過這些主題請幫助卡住無效的密鑰異常

Java.security.InvalidKeyException: Illegal key size or default parameters error

Java Security: Illegal key size or default parameters?

走了,但我還是被卡住,讓java.security.InvalidKeyException:非法密鑰大小或默認參數,

下面是我的代碼: AESKeyGenerator.java

public class AESKeyGenerator { 

    private Cipher mCipher; 

    public AESKeyGenerator() 
    { 
     // default constructor 
    } 


    public byte[] generate_k(String dhkey, String toEncrypt) 
    { 
     byte[] retVal; 

     try { // Set up the Cipher class of Android to use AES to generate keys 
      byte[] iv = new byte[16]; 
      for (int i = 0; i < iv.length; i++) 
       iv[i] = new Byte("0").byteValue(); 
      IvParameterSpec ivspec = new IvParameterSpec(iv); 
      mCipher = Cipher.getInstance("AES"); 
      // Set up key to use in algorithm 
      MessageDigest hasher = MessageDigest.getInstance("SHA-256"); // Initialize object that will hash my key. 
      byte[] key256 = hasher.digest(dhkey.getBytes()); // Hash the key to 256 bits using SHA 
      SecretKeySpec K = new SecretKeySpec(key256, "AES"); 
      System.out.println("SecretKeySpec : "+K + " key256 "+key256); 
      mCipher.init(Cipher.ENCRYPT_MODE, K, ivspec); 
      // Encrypt the parameter toEncrypt 
      retVal = mCipher.doFinal(toEncrypt.getBytes()); 
      return retVal; 
     } 
     catch (Exception e) { 
         e.printStackTrace(); 
      System.err.println("Could not create and initialize object Cipher."); 
     } 

     return null; 

    } 

    public byte[] generate_r(byte[] sharedKey, String toEncrypt) 
    { 
     byte[] retVal; 
     try { 
      /*byte[] iv = new byte[16]; 
      for (int i = 0; i < iv.length; i++) 
       iv[i] = new Byte("0").byteValue(); 
      IvParameterSpec ivspec = new IvParameterSpec(iv);*/ 

      // Set up the Cipher class of Android to use AES to generate keys 
      mCipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
      // Set up key to use in algorithm 
      MessageDigest hasher = MessageDigest.getInstance("SHA-256"); // Initialize object that will hash my key. 
      byte[] key256 = hasher.digest(sharedKey); // Hash the key to 256 bits using SHA 256 
      SecretKeySpec K = new SecretKeySpec(key256, "AES"); 
      mCipher.init(Cipher.ENCRYPT_MODE, K); 
      // Encrypt the parameter toEncrypt 
      System.out.println("toEncrypt AES: "+ toEncrypt); 
      retVal = mCipher.doFinal(toEncrypt.getBytes()); 
      return retVal; 
     } 
     catch (Exception e) { 
         e.printStackTrace(); 
      System.err.println("exception: "+ e.toString()); 
      System.err.println("Could not create and initialize object Cipher."); 
     } 

     return null; 

    } 
} 

我正在休耕錯誤:

java.security.InvalidKeyException: Illegal key size or default parameters 
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1010) 
    at javax.crypto.Cipher.implInit(Cipher.java:785) 
    at javax.crypto.Cipher.chooseProvider(Cipher.java:848) 
    at javax.crypto.Cipher.init(Cipher.java:1212) 
    at javax.crypto.Cipher.init(Cipher.java:1152) 
    at AESKeyGenerator.generate_r(AESKeyGenerator.java:74) 
    at DetectionServer.storeGridInformation(DetectionServer.java:309) 
    at DetectionServer.doPost(DetectionServer.java:103) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
exception: java.security.InvalidKeyException: Illegal key size or default parameters 
Could not create and initialize object Cipher. 

我的標準代碼,這也是與此相同檢查。我認爲配置或缺少庫存在一些問題。

+0

你有一個評論:''設置Android的Cipher類'。你是從哪裏複製/粘貼的,還是這個代碼在Android中運行? – 2013-03-01 08:33:06

回答

5

重現此 - 我已經安裝了當前Unlimited Strength Jurisdiction Policy Files,我已經使用了以下主要的方法來測試:

public static void main(String[] args) throws UnsupportedEncodingException { 
    AESKeyGenerator aes = new AESKeyGenerator(); 
    String sharedKey = "Bar12345Bar12345Bar12345Bar12345"; 
    aes.generate_r(sharedKey.getBytes("US-ASCII"), "Hello World"); 
} 

在安裝之前的政策文件,我得到了同樣的異常,因爲你。

我首先做錯的一件事是我將策略文件安裝到了Program files/jdk_1.7.0_13/jre/lib/security,但是使用的JRE位於Program files/jre7--因此請確保您已將策略文件安裝在正確的位置並檢查一個簡單的獨立的Java應用程序使用上面的主要方法,如果它工作。

+1

非常感謝Andreas,我在做同樣的錯誤,將策略文件安裝到Program files/jdk_1.7.0_13/jre/lib/security中,但是使用的JRE位於Program files/jre7中。我將這些文件安裝在files/jre7中它的作品:) – Raghvendra 2013-03-01 09:38:54