Honus Wagner
Honus Wagner

Reputation: 2908

EncryptionException: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

I have inherited an old java project from 2006 (the original dev is long gone, and I've never coded Java before) where I am getting this error:

EncryptionException: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

The code it references looks like this:

public String decrypt( String encryptedString ) throws EncryptionException
{
    if ( encryptedString == null || encryptedString.trim().length() <= 0 )
            throw new IllegalArgumentException( "encrypted string was null or empty" );

    try
    {
        SecretKey key = keyFactory.generateSecret( keySpec );
        cipher.init( Cipher.DECRYPT_MODE, key );
        BASE64Decoder base64decoder = new BASE64Decoder();
        byte[] cleartext = base64decoder.decodeBuffer( encryptedString );
        byte[] ciphertext = cipher.doFinal( cleartext );

        return bytes2String( ciphertext );
    }
    catch (Exception e)
    {
        throw new EncryptionException( e );
    }
}

I'm not entirely sure of the inner workings of the program, but I do know that in this project directory are a few config files, and a key.properties file. As far as "Input Lengths" go (as the error message refers to), my the password for the database is 15 chars long, and the "key" in key.properties is 25 chars long. I have no idea if that matters or not.

Things to note:

Thanks for all your help.

Upvotes: 1

Views: 12228

Answers (2)

erickson
erickson

Reputation: 269697

The input to which the error message refers is the cipher text (oddly-named cleartext), the result of the Base-64 decoding operation. Make sure that the encryptedString you are passing to this method decodes to a byte array with length that is a multiple of 8.

Upvotes: 5

Case
Case

Reputation: 1847

You should probably not change the JRE version unless you want to re-examine the code. I would try downgrading your JRE version on the new server before anything else, especially since the code previously worked.

Upvotes: 1

Related Questions