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*..)
开发盒和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 提供程序)时才有效。
I have a test which runs great on my development MacBook Pro, but fails to run in continuous integration TeamCity server.
The error is following:
java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
Both development box and TeamCity uses Java 1.6 and I use BouncyCastle library for the need of special AES encryption.
The code is following:
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"));
}
UPDATE
Looks like according to the selected answer I have to modify something on my TeamCity installation and it will possibly affect some user installations - so its not a good choice I have to switch to another crypto library to do that without limitations. So probably bouncy castle will help.
UPDATE 2
I actually switched to use BouncyCastle to avoid this limitation. Note this only works if you use own BC classes directly, not the BC provider.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
此错误意味着您的 Java 虚拟机使用的策略仅允许由于美国出口法律而受到限制的加密密钥大小。
Java 9 及更高版本
Java 9 中包含无限强度管辖策略文件并默认使用(请参阅 Java 9 迁移指南中的安全更新)。
如果您在 Java 9 中收到此错误,则可能意味着策略配置已更改为更严格的策略 (
limited
),请参阅迁移指南中的说明:Java 8 及更早版本
Java 8 Update 161 及更高版本
从 Java 8 Update 161 开始,Java 8 默认采用无限强度管辖权策略。如果您收到此错误,则可能表示配置已更改为
受限
。请参阅下一节有关 Java 8 Update 151 的说明或上一节有关 Java 9 的说明,将其更改回无限制
。Java 8 Update 151 及更高版本
从 Java 8 Update 151 开始,无限强度管辖权策略包含在 Java 8 中,但默认情况下不使用。要启用它,您需要编辑
/jre/lib/security
(对于 JDK)或中的
(对于 JRE)。取消注释(或包含)该行java.security
文件。 /lib/security确保您使用以管理员身份运行的编辑器编辑该文件。
策略更改仅在重新启动 JVM 后生效(这对于 Tomcat 等长时间运行的服务器进程尤其重要)。
为了向后兼容,安装下一节中记录的策略文件仍然有效。
在 Java 8 Update 151 之前
对于 Java 8 Update 144 及更早版本,您需要安装 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction 策略文件(可从 Oracle)。
要安装这些文件(来自下载中的
README.txt
):注意,对于 JDK,它位于 jre/lib/security 中。
新的策略文件只有在重新启动 JVM 后才会生效(这对于 Tomcat 等长时间运行的服务器进程尤其重要)。
This error means that your Java virtual machine uses a policy that only allows restricted cryptography key sizes due to US export laws.
Java 9 and higher
The Unlimited Strength Jurisdiction Policy Files are included with Java 9 and used by default (see Security Updates in the Java 9 Migration Guide).
If you get this error with Java 9, it might mean the policy configuration has been changed to a more restrictive policy (
limited
), see the instructions from the migration guide:Java 8 and earlier
Java 8 Update 161 and higher
Starting with Java 8 Update 161, Java 8 defaults to the Unlimited Strength Jurisdiction Policy. If you receive this error, it could indicate the configuration has been changed to
limited
. See instructions in the next section on Java 8 Update 151, or the previous section on Java 9, for changing this back tounlimited
.Java 8 Update 151 and higher
Starting with Java 8 Update 151, the Unlimited Strength Jurisdiction Policy is included with Java 8 but not used by default. To enable it, you need to edit the
java.security
file in<java_home>/jre/lib/security
(for JDK) or<java_home>/lib/security
(for JRE). Uncomment (or include) the lineMake sure you edit the file using an editor run as administrator.
The policy change only takes effect after restarting the JVM (this is especially important for long-running server processes like Tomcat).
For backwards compatibility, installing the policy files as documented in the next section will still work as well.
Before Java 8 Update 151
For Java 8 Update 144 and earlier, you need to install the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files (available at Oracle).
To install these files (from the
README.txt
in the download):Note for the JDK it is in jre/lib/security.
The new policy file only takes effect after restarting the JVM (this is especially important for long-running server processes like Tomcat).
我遇到了类似的问题,但就我而言,存在路径错误。
JAVA_HOME是jdk1.6.0_18,所以我将两个jar放入
jdk1.6.0_18/lib/security
中,但在jdk1.6.0_18内是jre
目录。这两个文件都应该放在jdk1.6.0_18/jre/lib/security
中。I had a similar problem, but in my case, there was a path error.
JAVA_HOME was jdk1.6.0_18, so I put the two jars into
jdk1.6.0_18/lib/security
, but within jdk1.6.0_18 is thejre
directory. Both files should have been put injdk1.6.0_18/jre/lib/security
.接受的答案假设您拥有管理员权限,可以以管理员身份修改文件和文件夹。
如果情况并非如此,对于 Java 8 Update 151 及更高版本,您可以执行以下操作:
crypto.policy=unlimited
的本地文件。-Djava.security.properties=
添加到您的 java 命令,其中
是您创建的本地文件此方法应该有效,除非
security .overridePropertiesFile
被显式禁用(默认情况下启用)。参考:/jre/lib/security/java.security
The accepted answer assumes that you have administrator permissions to modify files and folders as an administrator.
If that's not the case, for Java 8 Update 151 and higher you can do the following:
crypto.policy=unlimited
.-Djava.security.properties=<yourfile>
to your java command where<yourfile>
is the local file you createdThis method should work unless
security.overridePropertiesFile
is explicitly disabled (enabled by default).Reference:
<java_home>/jre/lib/security/java.security
除了安装策略文件之外,还要确保 CUSTOMLONGSECRETKEY...getBytes() 确实生成 32 字节数组。我将使用 CUSTOMLONGSECRETKEY.getBytes(some encoding) 并从中获取前 32 个字节。更好的是,使用整个密钥来派生具有您需要的大小的 AES 密钥。
In addition to installing policy files, also make sure that
CUSTOMLONGSECRETKEY...getBytes()
does indeed produce 32 bytes array. I would useCUSTOMLONGSECRETKEY.getBytes(some encoding)
and get first 32 bytes from that. Better yet, use whole secret key to derive keys for AES with the size that you need.我在 jdk 1.8.0_151 中遇到了同样的问题-
对于这个及以上版本,您不需要下载与安全相关的 jar 文件。因为 local_policy.jar 和 US_export_policy.jar 已经包含在这些版本的路径下-
\jre\lib\security\policy (JAVA_HOME 指的是你当前的java安装文件夹)
您需要进行的唯一更改是在 /jre/lib/security 中存在的 java.security 文件中 -
取消注释该行 -
crypto.policy=无限制
I was facing the same issue for jdk 1.8.0_151-
For this and above version, you do not need to download the jar files related to security.Because, local_policy.jar and US_export_policy.jar is already included in these versions under the path-
\jre\lib\security\policy (JAVA_HOME refers to your current java installation folder)
The only chng you need to make is in java.security file which is present in /jre/lib/security -
uncomment the line -
crypto.policy=unlimited
确保您知道 IDE 使用的 JAVA_HOME 路径。
以便复制到正确的路径。
就我而言,我使用 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
Make sure you know the path to JAVA_HOME that your IDE uses.
In order to copy to the correct path.
In my case I use IntelliJ:
/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/security
Instead of when i show the $JAVA_HOME in the console.
/Users/myuser/.sdkman/candidates/java/current/jre/lib/security