Java Cryptography Extension – Check Maximum Allowed Key Size

In Java, there is a limitation on key size by the JCE Jurisdiction Policy. If you manipulate a private key with bit size which is larger than the limitation, it will throw a InvalidKeyException complaining about Illegal key size.

The following piece of Java code will check maximum key size of all the algorithms and print the result to the console.

import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Set;
import javax.crypto.Cipher;

public class CheckKeySize {
	public static void main(String[] args) {
		try {
			Set<String> algorithms = Security.getAlgorithms("Cipher");
			for(String algorithm: algorithms) {
			    int max;
				max = Cipher.getMaxAllowedKeyLength(algorithm);
			    System.out.printf("%-22s: %dbit%n", algorithm, max);
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
}

 

Console output

BLOWFISH              : 128bit
ARCFOUR               : 128bit
PBEWITHMD5ANDDES      : 128bit
RC2                   : 128bit
RSA                   : 2147483647bit
PBEWITHMD5ANDTRIPLEDES: 128bit
PBEWITHSHA1ANDDESEDE  : 128bit
DESEDE                : 2147483647bit
AESWRAP               : 128bit
AES                   : 128bit
DES                   : 64bit
DESEDEWRAP            : 128bit
RSA/ECB/PKCS1PADDING  : 2147483647bit
PBEWITHSHA1ANDRC2_40  : 128bit

 

Done =)

Reference: セキュリティ

7 thoughts on “Java Cryptography Extension – Check Maximum Allowed Key Size”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.