Monday, April 4, 2011

Setup environment to use key size greater than 128 bits in JCE

When you are using Oracle default JCE provider or BouncyCastle provider, if you only use key size 64 or 128 bits, for BouncyCastle, you need to call:

Security.addProvider( new BouncyCastleProvider() );

for Oracle JCE provider, you don't need to add anything specifically.

Then you may call for example:

// This use the default ECB mode, and PKCS7Padding

Cipher cipher = Cipher.getInstance("AES", "BC");

So, it will the same as

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC" );

**** Note:

ECB has a problem to encrypt data, the cipher text shows the same pattern as the plain text, For example:

545454541234567854545454123443215454545412345678 2A8733982F64E6B35873778337192DA3931AE70A734C58761BC2A82498A133CC2A8733982F64E6B35873778337192DA33FE7286ABDE5F03943D5777020259626

More Details are here

The normal JDK download ships with a set of policy files that places certain restrictions on the key sizes that can be used. Key sizes are limited in general to 128 bits (except for the symmetric cipher Triple-DES) and RSA key generation is limited to 2,048 bits.

But if you want to use the key size greater than 128 bits, you have to download the unrestricted policy files, for Java 6, they are here:

After you unzip them you will get two JAR files:


Backup your existing files under the following location: $JAVA_HOME/lib/security

and then put the unzipped two files in this directory, now you could use the keys with size greater than 128.

If you don't do this, you will get the following exception:

Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..) at javax.crypto.Cipher.init(DashoA13*..) at javax.crypto.Cipher.init(DashoA13*..) at com.security.cert.security.symmtric.SimpleSymmetricExample.encrypt(SimpleSymmetricExample.java:37) at com.security.cert.security.symmtric.SimpleSymmetricExample.main(SimpleSymmetricExample.java:43) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)

Posted via email from Progress