【问题标题】:Please Help-Stuck Invalid Key Exception请帮助卡住无效的密钥异常
【发布时间】:2013-02-15 16:45:22
【问题描述】:

我收到 java.security.InvalidKeyException: Illegal key size or default parameters ,我已经完成了所有必需的步骤,安装了 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files。 我也经历过这些话题

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

Java Security: Illegal key size or default parameters?

但我仍然卡住并收到 java.security.InvalidKeyException: Illegal key size or default parameters ,

以下是我的代码: 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.

我检查了与此相同的标准代码。我认为配置或缺少库存在一些问题。

【问题讨论】:

  • 您有评论:// Set up the Cipher class of Android。您是从某个地方复制/粘贴的,还是这段代码在 Android 中运行?

标签: java security cryptography aes public-key-encryption


【解决方案1】:

我可以重现这个 - 我已经安装了当前的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 - 所以请确保您已将策略文件安装在正确的位置并检查如果可行,则使用上面的 main 方法使用简单的独立 java 应用程序。

【讨论】:

  • 非常感谢 Andreas,我也犯了同样的错误,将策略文件安装到 Program files/jdk_1.7.0_13/jre/lib/security,但使用的 JRE 位于 Program files/jre7 ...当我将这些文件安装在 files/jre7 中时,它的工作原理:)
【解决方案2】:

我遇到了同样的问题,但现在可以正常工作了,你必须去这个网站下载http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html 下载(UnlimitedJCEPolicyJDK7.zip)然后解压并将两个jar文件复制到您的jdk位置路径并重新启动您的项目。就是这样

【讨论】:

    猜你喜欢
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多