为了账号安全,请及时绑定邮箱和手机立即绑定

ElGamal加密算法异常

public static final String bcElGamal(String sourceChars) {

        // 公钥加密,私钥解密

        Security.addProvider(new BouncyCastleProvider());

        try {

            // 初始化密钥

            AlgorithmParameterGenerator algorithmParameterGenerator

                    = AlgorithmParameterGenerator.getInstance("ElGamal");

            algorithmParameterGenerator.init(256);

            AlgorithmParameters algorithmParameters 

                    = algorithmParameterGenerator.generateParameters();

            DHParameterSpec dhParameterSpec 

                    = algorithmParameters.getParameterSpec(DHParameterSpec.class);

            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ElGamal");

            keyPairGenerator.initialize(dhParameterSpec, new SecureRandom());

            KeyPair keyPair = keyPairGenerator.generateKeyPair();

            PublicKey elGamalPublicKey = keyPair.getPublic();

            PrivateKey elGamalPriveKey = keyPair.getPrivate();

            System.out.println("Public key :" + Base64.encodeBase64String(elGamalPublicKey.getEncoded()));

            System.out.println("Private key :" + Base64.encodeBase64String(elGamalPriveKey.getEncoded()));

            

            // 公钥加密,私钥解密——加密

            X509EncodedKeySpec x509EncodedKeySpec 

                    = new X509EncodedKeySpec(elGamalPublicKey.getEncoded());

            KeyFactory keyFactory = KeyFactory.getInstance("ElGamal");

            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

            Cipher cipher = Cipher.getInstance("ElGamal");

            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            byte[] result = cipher.doFinal(sourceChars.getBytes());

            System.out.println("bc elGamal encrypt :" + Base64.encodeBase64String(result));

            

            // 公钥加密,私钥解密——解密

            PKCS8EncodedKeySpec pKCS8EncodedKeySpec 

                    = new PKCS8EncodedKeySpec(elGamalPriveKey.getEncoded());

            keyFactory = KeyFactory.getInstance("ElGamal");

            PrivateKey privateKey = keyFactory.generatePrivate(pKCS8EncodedKeySpec);

            cipher = Cipher.getInstance("ElGamal");

            cipher.init(Cipher.DECRYPT_MODE, privateKey);

            result = cipher.doFinal(result);

            System.out.println("bc elGamal decrypt :" + new String(result));

             

            return null;

        } catch (NoSuchAlgorithmException ex) {

            throw new RuntimeException(ex);

        } catch (IllegalStateException ex) {

           throw new RuntimeException(ex);

        } catch (InvalidParameterSpecException ex) {

            throw new RuntimeException(ex);

        } catch (InvalidAlgorithmParameterException ex) {

            throw new RuntimeException(ex);

        } catch (InvalidKeyException ex) {

           throw new RuntimeException(ex);

        } catch (NoSuchPaddingException ex) {

            throw new RuntimeException(ex);

        } catch (IllegalBlockSizeException ex) {

            throw new RuntimeException(ex);

        } catch (BadPaddingException ex) {

           throw new RuntimeException(ex);

        } catch (InvalidKeySpecException ex) {

             throw new RuntimeException(ex);

        }

    }

此方法一直抛出异常:java.security.InvalidKeyException: Illegal key size or default parameters



正在回答

4 回答

使用AES加密时,当密钥大于128时,代码会抛出java.security.InvalidKeyException: Illegal key size or default parameters

Illegal key size or default parameters是指密钥长度是受限制的,java运行时环境读到的是受限的policy文件。文件位于${java_home}/jre/lib/security

这种限制是因为美国对软件出口的控制。


解决办法:

去掉这种限制需要下载Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.网址如下。

下载包的readme.txt 有安装说明。就是替换${java_home}/jre/lib/security/ 下面的local_policy.jar和US_export_policy.jar

jdk 5: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jce_policy-1.5.0-oth-JPR

jdk6: http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html


参考http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters

错误:java.security.InvalidKeyException: Illegal key size or default parameters解决方法

发布于 2014 年 3 月 18 日,属于 高性能JAVA  分类,757 浏览数

Java几乎各种常用加密算法都能找到对应的实现。因为美国的出口限制,Sun通过权限文件(local_policy.jar、US_export_policy.jar)做了相应限制。因此存在一些问题:
●密钥长度上不能满足需求(如:java.security.InvalidKeyException: Illegal key size or default parameters);
●部分算法未能支持,如MD4、SHA-224等算法;
●API使用起来还不是很方便;一些常用的进制转换辅助工具未能提供,如Base64编码转换、十六进制编码转换等工具。

    Oracle在其官方网站上提供了无政策限制权限文件(Unlimited Strength Jurisdiction Policy Files),我们只需要将其部署在JRE环境中,就可以解决限制问题。
下载地址:
      ●Java 5.0 无政策限制文件
      ●Java 6 无政策限制文件
      ●Java 7 无政策限制文件
      ●其他版本 无政策限制文件

      下载的压缩包中仅有一个目录,也就是jce目录。该目录中包含了4个文件:README.txt、COPYRIGHT.html、local_policy.jar和US_export_policy.jar。其中包含的两个jar文件正是此次配置中用到的文件。
      我们可以查看上述README.txt文件,你需要在JDK的JRE环境中,或者是JRE环境中配置上述两个jar文件。
      切换到%JDK_Home%\jre\lib\security目录下,对应覆盖local_policy.jar和US_export_policy.jar两个文件。同时,你可能有必要在%JRE_Home%\lib\security目录下,也需要对应覆盖这两个文件。
      配置权限文件的最终目的是为了使应用在运行环境中获得相应的权限,可以加强应用的安全性。通常,我们在应用服务器上安装的是JRE,而不是JDK。因此,这就很有必要在应用服务器的%JRE_Home%\lib\security目录下,对应覆盖这两个权限文件。很多开发人员往往忽略了这一点,导致事故发生。

AES加密时抛出java.security.InvalidKeyException: Illegal key size or default parameter

0条评论

[摘要:起源:http://blog.csdn.net/shangpusp/article/details/7416603 应用AES减稀时,当稀钥大于128时,代码会扔出java.security.InvalidKeyException: Illegal key size or default parameters Illegal key size or default parameters是指] 

来源:http://blog.csdn.net/shangpusp/article/details/7416603 

使用AES加密时,当密钥大于128时,代码会抛出java.security.InvalidKeyException: Illegal key size or default parameters


Illegal key size or default parameters是指密钥长度是受限制的,java运行时环境读到的是受限的policy文件。文件位于${java_home}/jre/lib/security 

这种限制是因为美国对软件出口的控制。 

解决办法: 

去掉这种限制需要下载Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.网址如下。 

下载包的readme.txt 有安装说明。就是替换${java_home}/jre/lib/security/ 下面的local_policy.jar和US_export_policy.jar 

jdk 5: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jce_policy-1.5.0-oth-JPR 

jdk6: http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html

jdk7下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html


AES加密参考:http://blog.csdn.net/hbcui1984/article/details/5201247 

感谢关注 Ithao123加密解密频道,ithao123.cn是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!

java.security.InvalidKeyException: Illegal key size or default parameters

时间 2014-06-27 11:06:58  CSDN博客

原文  http://blog.csdn.net/liwf_/article/details/35233009

主题 Java 网络安全

做CA认证 生成证书时候出错,后来发现是 秘钥长度太长了,怎么会有这个问题呢,看下面的:

参考网址 : http://open.eucalyptus.com/forum/illegal-key-size

http://ksgimi.iteye.com/blog/1584716

异常:

EjbcaException_Exception: exception encrypting data - java.security.InvalidKeyException: Illegal key size

分析:

Illegal key size or default parameters是指密钥长度是受限制的,java运行时环境读到的是受限的policy文件。文件位于${java_home}/jre/lib/security 

这种限制是因为美国对软件出口的控制。 

所以下载匹配的jce_policy ,替换jdk安装目录下 jdk1.* \jre\lib\security 中的 local_policy.jar  和 US_export_policy.jar 两个jar包。(不主要)

替换jdk安装目录下 jre *  \lib\security 中的 local_policy.jar  和 US_export_policy.jar 两个jar包。 (主要)

看下文:

I was working on webservice call where my code was breaking in RAD during decrypting the password of keystore. I encountered below error:


Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]


There are key size restrictions with the default crypto files local_policy.jar and US_export_policy.jar comes with JDK – which limits it to 128.   If your security policy using a key size larger than this – then the above exception is thrown.

For example – if your security policy specifies the algorithmic suite as Basic256 – then the key size to be used is 256.

For the solution of above issue, you need to patch your JDK with Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.

For JDK1.5  visit , download the crypto files and copy the two jar files from the extracted jce directory (local_policy.jar and US_export_policy.jar) to $JAVA_HOME/jre/lib/security.

For JDK1.6  visit

If your IDE using it’s own specific JDK then patch that as well with these files to resolve the issue.


0 回复 有任何疑惑可以回复我~

BTW:

 If ur JVM is IBM JVM pls refer to the below link to update the unlimited key size jars

http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.nd.multiplatform.doc%2Finfo%2Fae%2Fae%2Ftwbs_tunev6wss.html


0 回复 有任何疑惑可以回复我~

报错堆栈如下:


Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]    at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]    at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]    at my.package.Something.decode(RC4Decoder.java:25) ~[my.package.jar:na]


Google到问题原因,链接地址如下:

http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters

根据回答找到下载新jar包(JDK6)链接地址如下:

http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html

JDK7 的地址如下:

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

把里面的两个jar包:local_policy.jar 和 US_export_policy.jar 替换掉原来安装目录C:\Program Files\Java\jre6\lib\security 下的两个jar包接可以了

然后就重新运行程序,不会报错了,测试代码如下:


[java] view plain copy 

public class Test {  

    public static void main(String[] args) throws Exception {  

         KeyGenerator keyGen = KeyGenerator.getInstance("AES");  

         keyGen.init(256);  

         SecretKey key = keyGen.generateKey();  

         ObjectOutputStream oop = new ObjectOutputStream(new  

         FileOutputStream("c:\\key.dat"));  

         oop.writeObject(key);  

         oop.close();  

          

        String strTest = "Hello, Jason";  

        byte[] strAfterAES = encryptData(strTest.getBytes());  

        System.out.println(new String(strAfterAES));  

        byte[] strOriContent = decryptData(strAfterAES);  

        System.out.println(new String(strOriContent));  

    }  

  

  

    public static byte[] encryptData(byte[] input) throws Exception {  

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("c:\\key.dat"));  

        SecretKey aeskey = (SecretKey) in.readObject();  

        Cipher c1 = Cipher.getInstance("AES");  

        c1.init(Cipher.ENCRYPT_MODE, aeskey);  

        byte[] cipherByte = c1.doFinal(input);  

        return cipherByte;  

    }  

  

  

    public static byte[] decryptData(byte[] input) throws Exception {  

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("c:\\key.dat"));  

        SecretKey aeskey = (SecretKey) in.readObject();  

        Cipher c1 = Cipher.getInstance("AES");  

        c1.init(Cipher.DECRYPT_MODE, aeskey);  

        byte[] clearByte = c1.doFinal(input);  

        return clearByte;  

    }  

}  


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

ElGamal加密算法异常

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信