Java Security: Illegal key size or default parameters?

前端 未结 19 1455
日久生厌
日久生厌 2020-11-22 03:34

I had asked a question about this earlier, but it didn\'t get answered right and led nowhere.

So I\'ve clarified few details on the problem and I would really like t

相关标签:
19条回答
  • 2020-11-22 03:44

    the problem is the content of the file default_local.policy in local_policy.jar in the folder jre\lib\security, if you install the JRE:

    // Some countries have import limits on crypto strength. This policy file
    // is worldwide importable.
    
    grant {
        permission javax.crypto.CryptoPermission "DES", 64;
        permission javax.crypto.CryptoPermission "DESede", *;
        permission javax.crypto.CryptoPermission "RC2", 128,
                                         "javax.crypto.spec.RC2ParameterSpec", 128;
        permission javax.crypto.CryptoPermission "RC4", 128;
        permission javax.crypto.CryptoPermission "RC5", 128,
              "javax.crypto.spec.RC5ParameterSpec", *, 12, *;
        permission javax.crypto.CryptoPermission "RSA", *;
        permission javax.crypto.CryptoPermission *, 128;
    };
    

    if you do not need worldwide valid settings you simply can edit this file and change the content to

    // Country-specific policy file for countries with no limits on crypto strength.
    grant {
        // There is no restriction to any algorithms.
        permission javax.crypto.CryptoAllPermission;
    };
    

    this is what get if you download the JCE from Oracle.

    0 讨论(0)
  • 2020-11-22 03:44

    By default, Java only supports AES 128 bit (16 bytes) key sizes for encryption. If you do not need more than default supported, you can trim the key to the proper size before using Cipher. See javadoc for default supported keys.

    This is an example of generating a key that would work with any JVM version without modifying the policy files. Use at your own discretion.

    Here is a good article on whether key 128 to 256 key sizes matter on AgileBits Blog

    SecretKeySpec getKey() {
        final pass = "47e7717f0f37ee72cb226278279aebef".getBytes("UTF-8");
        final sha = MessageDigest.getInstance("SHA-256");
    
        def key = sha.digest(pass);
        // use only first 128 bit (16 bytes). By default Java only supports AES 128 bit key sizes for encryption.
        // Updated jvm policies are required for 256 bit.
        key = Arrays.copyOf(key, 16);
        return new SecretKeySpec(key, AES);
    }
    
    0 讨论(0)
  • 2020-11-22 03:45

    There's a short discussion of what appears to be this issue here. The page it links to appears to be gone, but one of the responses might be what you need:

    Indeed, copying US_export_policy.jar and local_policy.jar from core/lib/jce to $JAVA_HOME/jre/lib/security helped. Thanks.

    0 讨论(0)
  • 2020-11-22 03:47

    "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6"

    http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html

    0 讨论(0)
  • 2020-11-22 03:47

    If you are using Linux distribution with apt and have added webupd8 PPA, you can simply run the command

    apt-get install oracle-java8-unlimited-jce-policy
    

    Other updates:

    1. The Unlimited Strength Jurisdiction Policy Files are included with Java 9 and used by default
    2. Starting with Java 8 Update 161, Java 8 defaults to the Unlimited Strength Jurisdiction Policy.
    3. 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 line

      crypto.policy=unlimited

      Make sure you edit the file using an editor run as administrator. The policy change only takes effect after restarting the JVM

    Before Java 8 Update 151 rest of the answers hold valid. Download JCE Unlimited Strength Jurisdiction Policy Files and replace.

    For more details, you can refer to my personal blog post below - How to install Java Cryptography Extension (JCE) unlimited strength jurisdiction policy files

    0 讨论(0)
  • 2020-11-22 03:48

    I also got the issue but after replacing existing one with the downloaded (from JCE) one resolved the issue. New crypto files provided unlimited strength.

    0 讨论(0)
提交回复
热议问题