我怎样在Java代码中检查当前JVM是否具备无限制加密强度?
boolean limit = Cipher.getMaxAllowedKeyLength("RC5")<256;
因此,一个完整的程序可能是:
import javax.crypto.Cipher;
public class TestUCE {
public static void main(String args[]) throws Exception {
boolean unlimited =
Cipher.getMaxAllowedKeyLength("RC5") >= 256;
System.out.println("Unlimited cryptography enabled: " + unlimited);
}
}
如果您正在使用Linux,并且已安装了JDK(但Beanshell不可用),则可以使用JDK提供的runscript
命令进行检查。
jrunscript -e 'exit (javax.crypto.Cipher.getMaxAllowedKeyLength("RC5") >= 256 ? 0 : 1);'; echo $?
如果无限密码可用,则返回0
状态代码;如果不可用,则返回1
。对于shell函数来说,零是正确的“成功”返回值,非零表示失败。
jrunscript -e“exit(println(javax.crypto.Cipher.getMaxAllowedKeyLength(\”RC5\“)> = 256);)”
- chance我认为你可以使用Cipher.getMaxAllowedKeyLength(),同时将你正在使用的密码与已知的“好”的安全密码列表进行比较,例如AES。
这篇文章列出了截至Java 1.4时当前的最大密钥大小管辖限制(除非法律也发生了变化,否则这些可能没有改变-请参见下文)。
如果您所在的国家存在加密出口/进口限制,则必须查阅贵国的法律,但在这种情况下,您可能无法在JVM中默认使用无限强度加密。换句话说,假设您正在使用Oracle公司的官方JVM,并且您恰好居住在美国对其实施了加密出口限制的国家(由于Oracle是一家美国公司,因此会受到这些限制),那么在这种情况下,您还可以假设您没有无限强度可用。
当然,这并不妨碍您自己构建,从而授予自己无限的强度,但根据您所在地区的法律,这可能是非法的。
这篇文章概述了美国对其他国家加密出口的限制。
如何检查限制是否适用已在方法Cipher.getMaxAllowedKeyLength
中记录:
如果安装了JCE无限制强度管辖策略文件,则将返回
Integer.MAX_VALUE
。
这意味着,如果返回任何小于(或低于)Integer.MAX_VALUE
的值,则适用限制。
更多信息请参见下面方法的JavaDoc:
/**
* Determines if cryptography restrictions apply.
* Restrictions apply if the value of {@link Cipher#getMaxAllowedKeyLength(String)} returns a value smaller than {@link Integer#MAX_VALUE} if there are any restrictions according to the JavaDoc of the method.
* This method is used with the transform <code>"AES/CBC/PKCS5Padding"</code> as this is an often used algorithm that is <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#impl">an implementation requirement for Java SE</a>.
*
* @return <code>true</code> if restrictions apply, <code>false</code> otherwise
*/
public static boolean restrictedCryptography() {
try {
return Cipher.getMaxAllowedKeyLength("AES/CBC/PKCS5Padding") < Integer.MAX_VALUE;
} catch (final NoSuchAlgorithmException e) {
throw new IllegalStateException("The transform \"AES/CBC/PKCS5Padding\" is not available (the availability of this algorithm is mandatory for Java SE implementations)", e);
}
}
请注意,自Java 9以来,默认安装无限制的加密策略(那些受进出口管制影响的人需要安装有限的加密策略)。因此,这段代码主要是为了向后兼容和/或其他运行时所需。getMaxAllowedKeyLength("foo")
并仍然得到正确的答案。我发现只要参数不是 null
或 ""
,您就会得到您要寻找的答案。 - Christopher Schultz这是完整的复制粘贴版本,以便进行测试。
import javax.crypto.Cipher;
import java.security.NoSuchAlgorithmException;
class Test {
public static void main(String[] args) {
int allowedKeyLength = 0;
try {
allowedKeyLength = Cipher.getMaxAllowedKeyLength("AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
System.out.println("The allowed key length for AES is: " + allowedKeyLength);
}
}
运行以下命令:
javac Test.java
java Test
如果 JCE 未能正常工作,输出结果为:128
。如果 JCE 已经正常工作,输出结果会类似于:2147483647
。
getMaxAllowedKeyLength
可能不会返回有效的 AES 密钥大小,因此此答案是不正确的。个人而言,我讨厌 printStackTrace
,因为代码会继续运行。如果您抛出 RuntimeException
,则无需将错误值 0 分配给 allowedKeyLength
。 - Maarten Bodewes如果您使用的是Linux系统,可以通过以下命令轻松检查:
java -version ; \
echo 'System.err.println(javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding").getMaxAllowedKeyLength("AES"));' \
| java -cp /usr/share/java/bsh-*.jar bsh.Interpreter >/dev/null
java version "1.7.0_76"
Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
128
你可以使用Groovy在命令行中一步检查它:
groovysh -e 'javax.crypto.Cipher.getMaxAllowedKeyLength("AES")'
如果结果是2147483647
,则您拥有无限的加密功能。
在旧版本的Groovy中,您需要删除-e
:
groovysh 'javax.crypto.Cipher.getMaxAllowedKeyLength("AES")'
注意:请使用jefflunt的答案或KonstantinSpirov的答案。这个答案不是一个有效的答案,因为它总是返回true
。我只是把这个答案留在这里,因为它在其他答案和评论中被引用,并且仅作为参考。
static final boolean
,然后在需要测试无限制加密支持的时候使用它(因为只有安装了无限制策略才支持AES 256位)。boolean isUnlimitedSupported = false;
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES", "SunJCE");
kgen.init(256);
isUnlimitedSupported = true;
} catch (NoSuchAlgorithmException e) {
isUnlimitedSupported = false;
} catch (NoSuchProviderException e) {
isUnlimitedSupported = false;
}
System.out.println("isUnlimitedSupported=" + isUnlimitedSupported);
// set static final variable = isUnlimitedSupported;
Cipher
检查允许使用的密钥大小,而KeyGenerator
则没有这样做。这些限制并非特定于提供程序。此外,实例化KeyGenerator
不是检查这些限制的高效或逻辑方法。请删除不正确的答案。 - Maarten Bodewes最近我不得不添加一个JCE检查,我的解决方案演变成了以下片段。这是一个Groovy脚本,但应该很容易转换为标准的Java方法,并带有try catch。这已经在Java 7和Java 8中进行了测试。
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.SecretKey;
// Make a blank 256 Bit AES Key
final SecretKey secretKey = new SecretKeySpec(new byte[32], "AES");
final Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// This line will throw a invalid key length exception if you don't have
// JCE Unlimited strength installed
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
// If it makes it here, you have JCE installed