小智 43
本着与丹克鲁兹的答案相同的精神,但只有一行代码而没有通过例外:
boolean limit = Cipher.getMaxAllowedKeyLength("RC5")<256;
Run Code Online (Sandbox Code Playgroud)
所以一个完整的程序可能是:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 35
如果您使用的是Linux并且已安装JDK(但Beanshell不可用),则可以runscript使用JDK提供的命令进行检查.
jrunscript -e 'exit (javax.crypto.Cipher.getMaxAllowedKeyLength("RC5") >= 256 ? 0 : 1);'; echo $?
Run Code Online (Sandbox Code Playgroud)
0如果Unlimited Cryptography可用,或者1如果不可用,则返回状态代码.零是shell函数的正确"成功"返回值,非零表示失败.
jef*_*unt 25
我认为您可以使用Cipher.getMaxAllowedKeyLength(),同时还将您正在使用的密码与已知的"好"安全密码列表进行比较,例如AES.
这是一篇参考文章,其中列出了Java 1.4中最新的密钥大小管辖权限制(这些限制可能没有改变,除非法律也发生了变化 - 见下文).
如果您在一个具有加密出口/进口限制的国家/地区运营,您必须咨询您所在国家/地区的法律,但在这些情况下您可能会安全地假设您没有可用的无限强度加密(默认情况下)在你的JVM中.换句话说,假设您正在使用Oracle的官方JVM,并且您碰巧生活在一个美国已经对加密出口限制的国家(并且由于Oracle是一家美国公司,它将受制于这些国家限制),那么在这种情况下你也可以假设你没有无限的力量可用.
当然,这并不能阻止你建立自己的,从而给予自己无限的力量,但根据你当地的法律,这可能是非法的.
本文概述了从美国出口到其他国家的限制.
Maa*_*wes 16
方法中记录了如何检查是否适用限制的方法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);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,自Java 9以来,默认情况下安装了无限制加密策略(受导入/导出规则影响的那些策略必须安装有限的加密策略).因此,此代码主要用于向后兼容性和/或其他运行时.
这是一个完整的复制粘贴版本,允许进行测试
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);
}
}
Run Code Online (Sandbox Code Playgroud)
跑步
javac Test.java
java Test
如果JCE没有工作输出:128
JCE的工作方式如下:2147483647