Key length must be between 40 and 1024 bit

1.4k views Asked by At

I am getting following exception in my password encryption and decryption java file.

I got the solution for this issue in most of the website is replace existing security policy file with Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.

I did that also but still the same error is coming.

But the same code is working fine in jdk1.6 with Linux OS. I have copied latest 1.7 compatible Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files from website.

JDK Version - jdk1.7.0_25
Server OS - Linux

Exception:
java.security.InvalidKeyException: Key length must be between 40 and 1024 bit

1

There are 1 answers

4
Tilman Hausherr On

You explained in the comments that the key passed is 512 bytes long. The error message is "Key length must be between 40 and 1024 bit".

512 bytes * 8 bit = 4096 bits. Your key is indeed too long.

If you're not decoding stuff that was encoded earlier with keys longer than 1024 bits, then use this code to cut off at 128 bytes (= 1024 bits):

byte[] key = HexDecode(s1);
if (key.length > 128)
{
    key = Arrays.copyOf(key, 128);
}
SecretKeySpec secretkeyspec = new SecretKeySpec(key, "RC4");