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,开发者_JS百科 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:
- I have tried changing the DB password to 16 chars (a multiple of 8) but to no avail.
- I have read this and this and they have not helped
- I am moving this project from one server to another. It works on its original server.
- The original server runs JRE 1.4.2. The new server runs JRE 1.6u27.
- I REALLY don't want to have to rebuild the .jar. I am not a java developer and the project is pretty massive.
Thanks for all your help.
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.
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.
精彩评论