有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java InvalidKeyException非法密钥大小

我有一个测试在我的开发MacBook Pro上运行得很好,但在持续集成TeamCity服务器上运行失败

错误如下:

java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)

development box和TeamCity都使用Java 1.6,我使用BouncyCastle库来满足特殊AES加密的需要

代码如下:

private byte[] aesEncryptedInfo(String info) throws UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidParameterSpecException, InvalidAlgorithmParameterException, NoSuchProviderException {
    Security.addProvider(new BouncyCastleProvider());
    SecretKey secret = new SecretKeySpec(CUSTOMLONGSECRETKEY.substring(0, 32).getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(VECTOR_SECRET_KEY.getBytes()));
    return cipher.doFinal(info.getBytes("UTF-8"));
}

更新

看起来,根据选择的答案,我必须修改TeamCity安装中的某些内容,这可能会影响某些用户安装-因此,这不是一个好选择,我必须切换到另一个加密库才能做到这一点,没有限制。所以,弹跳城堡可能会有所帮助

更新2

实际上,我改用BouncyCastle来避免这个限制。注意,这仅在您直接使用自己的BC类而不是BC提供程序时有效


共 (5) 个答案

  1. # 1 楼答案

    除了安装策略文件外,还要确保CUSTOMLONGSECRETKEY...getBytes()确实生成32字节数组。我将使用CUSTOMLONGSECRETKEY.getBytes(some encoding)并从中获取前32个字节。更好的是,使用完整的密钥为AES派生出您所需大小的密钥

  2. # 2 楼答案

    此错误意味着您的Java虚拟机使用的策略仅允许受美国出口法限制的加密密钥大小

    Java 9及更高版本

    无限制强度管辖权策略文件包含在Java 9中,默认情况下使用(请参见Security Updates in the Java 9 Migration Guide

    如果Java 9出现此错误,可能意味着策略配置已更改为限制性更强的策略(limited),请参阅《迁移指南》中的说明:

    JCE Jurisdiction Policy File Default is Unlimited

    If your application previously required the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files, then you no longer need to download or install them. They are included in the JDK and are activated by default.

    If your country or usage requires a more restrictive policy, the limited Java cryptographic policy files are still available.

    If you have requirements that are not met by either of the policy files provided by default, then you can customize these policy files to meet your needs.

    See the crypto.policy Security property in the <java-home>/conf/security/java.security file, or Cryptographic Strength Configuration in the Java Platform, Standard Edition Security Developer's Guide.

    Java8及更早版本

    Java 8更新161及更高版本

    从Java8更新161开始,Java8默认为无限强度管辖权策略。如果收到此错误,则可能表明配置已更改为limited。有关将其更改回unlimited的信息,请参阅下一节关于Java8更新151的说明,或上一节关于Java9的说明

    Java 8更新151及更高版本

    从Java 8 Update 151开始,Java 8中包含无限强度管辖权策略,但默认情况下不使用。要启用它,您需要在<java_home>/jre/lib/security(对于JDK)或<java_home>/lib/security(对于JRE)中编辑java.security文件。取消注释(或包括)行

    crypto.policy=unlimited
    

    确保使用以管理员身份运行的编辑器编辑文件

    策略更改仅在重新启动JVM后生效(这对于像Tomcat这样的长时间运行的服务器进程尤其重要)

    为了向后兼容,安装下一节中介绍的策略文件仍然可以

    Java 8更新之前151

    对于Java8Update144和更早版本,您需要安装Java加密扩展(JCE)无限强度权限策略文件(可在Oracle上获得)

    要安装这些文件(从下载中的README.txt),请执行以下操作:

    1. Download the unlimited strength JCE policy files.

    2. Uncompress and extract the downloaded file.

      This will create a subdirectory called jce. This directory contains the following files:

      README.txt                   This file
      local_policy.jar             Unlimited strength local policy file
      US_export_policy.jar         Unlimited strength US export policy file
      
    3. Install the unlimited strength policy JAR files.

      In case you later decide to revert to the original "strong" but limited policy versions, first make a copy of the original JCE policy files (US_export_policy.jar and local_policy.jar). Then replace the strong policy files with the unlimited strength versions extracted in the previous step.

      The standard place for JCE jurisdiction policy JAR files is:

      <java-home>/lib/security           [Unix]
      <java-home>\lib\security           [Windows]
      

    注意,对于JDK,它位于jre/lib/security中

    新的策略文件只有在重新启动JVM后才生效(这对于像Tomcat这样的长时间运行的服务器进程尤其重要)

  3. # 3 楼答案

    我有一个类似的问题,但在我的情况下,有一个路径错误

    JAVA_的家是jdk1。6.0_18,所以我将这两个jar放在jdk1.6.0_18/lib/security中,但放在jdk1中。6.0_18是jre目录。这两个文件都应该放在jdk1.6.0_18/jre/lib/security

  4. # 4 楼答案

    对于jdk 1.8.0_151,我也面临同样的问题-

    对于这个及以上版本,您不需要下载与安全性相关的jar文件。因为,地方政策。jar和美国出口政策。jar已经包含在这些版本的路径下- \jre\lib\security\policy(JAVA\u HOME指当前的JAVA安装文件夹) 您只需要在java中进行更改。存在于/jre/lib/security中的安全文件- 取消对该行的注释- 加密。策略=无限制

  5. # 5 楼答案

    确保您知道IDE使用的JAVA\u主页路径。 以便复制到正确的路径

    就我而言,我使用IntelliJ: /Library/Java/JavaVirtualMachines/jdk1。8.0_112.jdk/Contents/Home/jre/lib/security

    而不是在控制台中显示$JAVA_HOME时。 /Users/myuser/。sdkman/candidates/java/current/jre/lib/security