JCE Unlimited Strength installed but AES 256 is no

2020-06-29 19:11发布

问题:

I have installed JCE Unlimited strength to JAVA_HOME\lib\security
However, I'm still getting 128 for Cipher.getMaxAllowedKeyLength("AES").

I'm wondering if I have installed the JCE at the wrong place.
I have Java installed in 2 places.

  1. C:\Program Files\Java\jre7
  2. C:\Development\Java\jdk1.6.0_21

Can anyone tell me where is the correct place to install JCE Unlimited strength? Your help is much appreciated.

my code:

 KeyGenerator generator = KeyGenerator.getInstance("AES");      
 generator.init(256);       SecretKey secretKey = generator.generateKey();      
 byte[] raw= secretKey.getEncoded();        
 SecretKeySpec sskey= new SecretKeySpec(raw, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");         
        if (mode == Cipher.ENCRYPT_MODE) { 
           Cipher.getMaxAllowedKeyLength("AES"));   
           cipher.init(Cipher.ENCRYPT_MODE, sskey);             
           CipherInputStream cis = new CipherInputStream(is, cipher); 
           doCopy(cis, os);         
        } else if (mode == Cipher.DECRYPT_MODE) { 
           cipher.init(Cipher.DECRYPT_MODE, sskey); 
           CipherOutputStream cos = new CipherOutputStream(os, cipher);             
           doCopy(is, cos);         
        }

回答1:

You need to install the files into whichever JVM is going to run your code. To be on the safe side, I'd advocate installing it in both.

I notice you have two different versions: Java 7 for a JRE and Java 6 for an SDK. Bear in mind that Java 6 and 7 have different unlimited strength policy files, so you'll need to download both sets.

  • For the JRE, install into C:\Program Files\Java\jre7\lib\security.
  • For the JDK, install into C:\Development\Java\jdk1.6.0_21\jre\lib\security.