zoukankan      html  css  js  c++  java
  • java中的AES 256算法遇到 Illegal key size or default parameters错的解决办法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
    内部邀请码:C8E245J (不写邀请码,没有现金送)
    国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为中国PE第一股,市值超1000亿元。 

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------

     

    报错堆栈如下:

    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包链接地址如下:

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

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

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

    [java] view plaincopy
     
    1. public class Test {  
    2.     public static void main(String[] args) throws Exception {  
    3.          KeyGenerator keyGen = KeyGenerator.getInstance("AES");  
    4.          keyGen.init(256);  
    5.          SecretKey key = keyGen.generateKey();  
    6.          ObjectOutputStream oop = new ObjectOutputStream(new  
    7.          FileOutputStream("c:\key.dat"));  
    8.          oop.writeObject(key);  
    9.          oop.close();  
    10.           
    11.         String strTest = "Hello, Jason";  
    12.         byte[] strAfterAES = encryptData(strTest.getBytes());  
    13.         System.out.println(new String(strAfterAES));  
    14.         byte[] strOriContent = decryptData(strAfterAES);  
    15.         System.out.println(new String(strOriContent));  
    16.     }  
    17.   
    18.   
    19.     public static byte[] encryptData(byte[] input) throws Exception {  
    20.         ObjectInputStream in = new ObjectInputStream(new FileInputStream("c:\key.dat"));  
    21.         SecretKey aeskey = (SecretKey) in.readObject();  
    22.         Cipher c1 = Cipher.getInstance("AES");  
    23.         c1.init(Cipher.ENCRYPT_MODE, aeskey);  
    24.         byte[] cipherByte = c1.doFinal(input);  
    25.         return cipherByte;  
    26.     }  
    27.   
    28.   
    29.     public static byte[] decryptData(byte[] input) throws Exception {  
    30.         ObjectInputStream in = new ObjectInputStream(new FileInputStream("c:\key.dat"));  
    31.         SecretKey aeskey = (SecretKey) in.readObject();  
    32.         Cipher c1 = Cipher.getInstance("AES");  
    33.         c1.init(Cipher.DECRYPT_MODE, aeskey);  
    34.         byte[] clearByte = c1.doFinal(input);  
    35.         return clearByte;  
    36.     }  
    37. }  

    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

  • 相关阅读:
    ISBN号码
    计数问题
    小玉在游泳
    数字反转
    单调队列(学习笔记)
    LCA(学习笔记)
    emacs配置文件
    线段树(学习笔记)
    RMQ问题 ST算法(学习笔记)
    Lucas卢卡斯定理(学习笔记)
  • 原文地址:https://www.cnblogs.com/AloneSword/p/3487809.html
Copyright © 2011-2022 走看看