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
2016-03-12
使用AES加密時,當(dāng)密鑰大于128時,代碼會拋出java.security.InvalidKeyException: Illegal key size or default parameters
Illegal key size or default parameters是指密鑰長度是受限制的,java運(yùn)行時環(huán)境讀到的是受限的policy文件。文件位于${java_home}/jre/lib/security
這種限制是因為美國對軟件出口的控制。
解決辦法:
去掉這種限制需要下載Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.網(wǎng)址如下。
下載包的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解決方法
發(fā)布于?2014 年 3 月 18 日,屬于?高性能JAVA??分類,757?瀏覽數(shù)
Java幾乎各種常用加密算法都能找到對應(yīng)的實現(xiàn)。因為美國的出口限制,Sun通過權(quán)限文件(local_policy.jar、US_export_policy.jar)做了相應(yīng)限制。因此存在一些問題:
●密鑰長度上不能滿足需求(如:java.security.InvalidKeyException: Illegal key size or default parameters);
●部分算法未能支持,如MD4、SHA-224等算法;
●API使用起來還不是很方便;一些常用的進(jìn)制轉(zhuǎn)換輔助工具未能提供,如Base64編碼轉(zhuǎn)換、十六進(jìn)制編碼轉(zhuǎn)換等工具。
????Oracle在其官方網(wǎng)站上提供了無政策限制權(quán)限文件(Unlimited Strength Jurisdiction Policy Files),我們只需要將其部署在JRE環(huán)境中,就可以解決限制問題。
下載地址:
??????●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環(huán)境中,或者是JRE環(huán)境中配置上述兩個jar文件。
??????切換到%JDK_Home%\jre\lib\security目錄下,對應(yīng)覆蓋local_policy.jar和US_export_policy.jar兩個文件。同時,你可能有必要在%JRE_Home%\lib\security目錄下,也需要對應(yīng)覆蓋這兩個文件。
??????配置權(quán)限文件的最終目的是為了使應(yīng)用在運(yùn)行環(huán)境中獲得相應(yīng)的權(quán)限,可以加強(qiáng)應(yīng)用的安全性。通常,我們在應(yīng)用服務(wù)器上安裝的是JRE,而不是JDK。因此,這就很有必要在應(yīng)用服務(wù)器的%JRE_Home%\lib\security目錄下,對應(yīng)覆蓋這兩個權(quán)限文件。很多開發(fā)人員往往忽略了這一點,導(dǎo)致事故發(fā)生。
AES加密時拋出java.security.InvalidKeyException: Illegal key size or default parameter
0條評論
[摘要:起源:http://blog.csdn.net/shangpusp/article/details/7416603 應(yīng)用AES減稀時,當(dāng)稀鑰大于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加密時,當(dāng)密鑰大于128時,代碼會拋出java.security.InvalidKeyException: Illegal key size or default parameters
Illegal key size or default parameters是指密鑰長度是受限制的,java運(yùn)行時環(huán)境讀到的是受限的policy文件。文件位于${java_home}/jre/lib/security?
這種限制是因為美國對軟件出口的控制。?
解決辦法:?
去掉這種限制需要下載Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.網(wǎng)址如下。?
下載包的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?
感謝關(guān)注 Ithao123加密解密頻道,ithao123.cn是專門為互聯(lián)網(wǎng)人打造的學(xué)習(xí)交流平臺,全面滿足互聯(lián)網(wǎng)人工作與學(xué)習(xí)需求,更多互聯(lián)網(wǎng)資訊盡在 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?網(wǎng)絡(luò)安全
做CA認(rèn)證 生成證書時候出錯,后來發(fā)現(xiàn)是 秘鑰長度太長了,怎么會有這個問題呢,看下面的:
參考網(wǎng)址 :?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運(yùn)行時環(huán)境讀到的是受限的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.
2016-03-12
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
2016-03-12
報錯堆棧如下:
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
根據(jù)回答找到下載新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包接可以了
然后就重新運(yùn)行程序,不會報錯了,測試代碼如下:
[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;??
????}??
} ?