检查Unlimited Cryptography是否可用

Chi*_*Lan 44 java jce

如何在Java代码中检查当前JVM是否具有可用的无限强度加密?

小智 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)

  • 这是比IMO接受的答案更好的答案. (3认同)
  • 这个答案使用了`"RC5"`变换,一个相当模糊的流密码*不在所需算法列表中*,它检查256(可能在将来受限制的JRE中可用,它也不处理异常)好的.请看一下解决这个问题的[我的回答](http://stackoverflow.com/a/33849265/589259). (2认同)

小智 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函数的正确"成功"返回值,非零表示失败.

  • Windows 版本: `jrunscript -e "exit (println(javax.crypto.Cipher.getMaxAllowedKeyLength(\"RC5\") &gt;= 256));"` (2认同)

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以来,默认情况下安装了无限制加密策略(受导入/导出规则影响的那些策略必须安装有限的加密策略).因此,此代码主要用于向后兼容性和/或其他运行时.


cod*_*ode 5

这是一个完整的复制粘贴版本,允许进行测试

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