JCE无限制权限策略文件

断鸿 2019年07月15日 84次浏览

使用JDK中AES对称加解密时,如果密钥大于128,则会抛出这个异常。这是因为美国的软件出口限制,Sun通过权限文件(local_policy.jar、US_export_policy.jar)做了相应限制。密钥长度最大128。解决方法是下载Oracle官方网站上的无政策限制权限文件,下载后将%JAVA_HOME%\jre\lib\security中的local_policy.jarUS_export_policy.jar替换为下载包中的的local_policy.jarUS_export_policy.jar

Caused by: java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size or default parameters
        at com.btzh.util.AESUtil.encrypt(AESUtil.java:18)
        at com.btzh.service.impl.SiThirdSystemServiceImpl.mapSystemParams(SiThirdSystemServiceImpl.java:32)
        at com.btzh.resource.SiThirdSystemResource.mapSystemParams(SiThirdSystemResource.java:41)
        ... 43 common frames omitted
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
        at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1026)
        at javax.crypto.Cipher.implInit(Cipher.java:801)
        at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
        at javax.crypto.Cipher.init(Cipher.java:1249)
        at javax.crypto.Cipher.init(Cipher.java:1186)
        at com.btzh.util.AESUtil.encrypt(AESUtil.java:15)