報錯堆棧以下:html
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到問題緣由,連接地址以下:java
http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parametersweb
根據回答找到下載新jar包連接地址以下:oracle
http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.htmljsp
把裏面的兩個jar包:local_policy.jar 和 US_export_policy.jar 替換掉原來安裝目錄C:\Program Files\Java\jre6\lib\security 下的兩個jar包接能夠了oop
而後就從新運行程序,不會報錯了,測試代碼以下:測試
public class Test { spa
public static void main(String[] args) throws Exception { code
KeyGenerator keyGen = KeyGenerator.getInstance("AES"); orm
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;
}
}
BTW:
If ur JVM is IBM JVM pls refer to the below link to update the unlimited key size jars