请帮助卡住无效键异常

时间:2022-01-23 18:30:35

I am getting java.security.InvalidKeyException: Illegal key size or default parameters , I have fallow all the required step ,Installed Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files. Also I have Gone through these Threads

我得到java.security。InvalidKeyException:非法密钥大小或默认参数,我暂停所有必需的步骤,安装Java加密扩展(JCE)无限制强度权限策略文件。我也读过这些线索

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

Java.security。InvalidKeyException:非法密钥大小或默认参数错误。

Java Security: Illegal key size or default parameters?

Java安全性:非法密钥大小还是默认参数?

But I am still stuck and getting java.security.InvalidKeyException: Illegal key size or default parameters ,

但我还是被java.security困住了。InvalidKeyException:非法密钥大小或默认参数,

Below is My Code : AESKeyGenerator.java

下面是我的代码: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;

    }
}

I am getting fallowing error:

我有一个错误:

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.

I check with the standard code which is also same as this. I think there is some issue with configuration or missing library.

我用与此相同的标准代码进行检查。我认为配置或缺少库会有问题。

2 个解决方案

#1


5  

I can not reproduce this - I have installed the current Unlimited Strength Jurisdiction Policy Files and I have used the following main method to test:

我不能复制这个-我已经安装了目前的无限力量管辖政策文件,我使用了以下主要方法来测试:

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

Prior to installing the policy files, I got the same exception as you.

在安装策略文件之前,我得到了与您相同的异常。

One thing I did wrong first is that I installed the policy files into Program files/jdk_1.7.0_13/jre/lib/security, but the JRE which was used is located in Program files/jre7 - so make sure that you have installed the policy files in the proper location and check with a simple standalone java application using the main method above if it works.

我做错了一件事首先,我安装了政策文件到程序文件/ jdk_1.7.0_13 / jre / lib /安全,但使用的jre位于程序文件/ jre7 -所以确保你有政策文件安装在适当的位置和检查用一个简单的独立的java应用程序使用上面的主要方法,如果它的工作原理。

#2


1  

i got the same problem but it works fine now,,you have to go to this site and download http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html download(UnlimitedJCEPolicyJDK7.zip) then extract it and copy the two jar file to your jdk location path and restart your project. that's it

我遇到了同样的问题,但它现在运行良好,您必须访问这个站点并下载http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html下载(UnlimitedJCEPolicyJDK7.zip),然后提取它并将两个jar文件复制到您的jdk位置路径并重新启动您的项目。就是这样

#1


5  

I can not reproduce this - I have installed the current Unlimited Strength Jurisdiction Policy Files and I have used the following main method to test:

我不能复制这个-我已经安装了目前的无限力量管辖政策文件,我使用了以下主要方法来测试:

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

Prior to installing the policy files, I got the same exception as you.

在安装策略文件之前,我得到了与您相同的异常。

One thing I did wrong first is that I installed the policy files into Program files/jdk_1.7.0_13/jre/lib/security, but the JRE which was used is located in Program files/jre7 - so make sure that you have installed the policy files in the proper location and check with a simple standalone java application using the main method above if it works.

我做错了一件事首先,我安装了政策文件到程序文件/ jdk_1.7.0_13 / jre / lib /安全,但使用的jre位于程序文件/ jre7 -所以确保你有政策文件安装在适当的位置和检查用一个简单的独立的java应用程序使用上面的主要方法,如果它的工作原理。

#2


1  

i got the same problem but it works fine now,,you have to go to this site and download http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html download(UnlimitedJCEPolicyJDK7.zip) then extract it and copy the two jar file to your jdk location path and restart your project. that's it

我遇到了同样的问题,但它现在运行良好,您必须访问这个站点并下载http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html下载(UnlimitedJCEPolicyJDK7.zip),然后提取它并将两个jar文件复制到您的jdk位置路径并重新启动您的项目。就是这样