5

I've been told to encrypt some form data (strings) using AES256 and was given a nice library which already does it all. I was just trying to make sure I understand it a bit better and learn a bit about encryption since it isn't something I am ever comfortable with. While doing that I ran a test I saw on some website, it said to call this Cipher.getMaxAllowedKeyLength("AES") which gives you the maximum key length. The result was 128.

Anyways the max allowed key length is 128, does that mean I can not use AES256? or are those unrelated?

EDIT: I should mention that I do know how to get the unlimited policy files to change this, I'm just trying to understand this whole deal better before proceeding.

casolorz
  • 8,486
  • 19
  • 93
  • 200

2 Answers2

4

For US export restriction reasons, Java ships with 128-bit security by default only. You need to download and install the Java Cryptography Extension if you want to work with 256-bit+ security.

John Farrelly
  • 7,289
  • 9
  • 42
  • 52
  • The page you link says it's due to *import* restrictions, not *export* restrictions. – CodesInChaos Jun 10 '14 at 08:13
  • So just to confirm, even though the code looks like this: `KeyGenerator kgen = KeyGenerator.getInstance("AES");kgen.init(256);` but the method `getMaxAllowedKeyLength()` returns 128, that means it is really encrypting just using a 128 key length, not a 256 key length? Thanks. – casolorz Jun 10 '14 at 15:59
3

The getMaxAllowedKeyLength() has been introduced just for this purpose, otherwise you would have to handle an exception during the Cipher encryption/decryption operations (update and doFinal) to test if the restrictions apply. As the policy files may change in time or for different versions of Java, it is easier to test with a method.

Note that getMaxAllowedKeyLength() should not be used for any other reason than testing for restrictions. Notably, it may well return Integer.MAX_VALUE instead of a valid key size.

And of course, if it returns 128 you cannot use AES with a key size of 256.


To remedy this you need to install the Unlimited Strength Jurisdiction Policy Files for the Oracle JRE / JDK and then copy it into the (jre)/lib/security folder of all the Java installations where you want to use larger key sizes. You can overwrite the files that are already there. You may need local admin rights or similar rights on that folder to do so.

If that is not possible you could use another implementation of AES that doesn't require the Cipher class as this class actually enforces the limitations. There are a few tricks around this issue as well.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I'm not sure which part of this is not made clear in the API, but there it is. – Maarten Bodewes Jun 09 '14 at 21:45
  • Just to be clear, where would the exception occur? the code in which I did the `getMaxAllowedKeyLength()` has the following when initializing the encryption library `KeyGenerator kgen = KeyGenerator.getInstance("AES");kgen.init(256);` and it seems to encrypt just fine after that even though `getMaxAllowedKeyLength()` returned 128. I just want to make sure I understand this a bit better before I bring this up to my superiors. – casolorz Jun 10 '14 at 15:57
  • An exception for key sizes higher than allowed occurs in the `Cipher` class (not the `CipherSpi` class that is implemented by providers), when you are using it. I've already updated the answer to include this information. – Maarten Bodewes Jun 10 '14 at 16:10