EncryptionException: javax.crypto.IllegalBlockSizeException: input length must be a multiple of 8 when decrypting using augmented cipher

I inherited an old java project from 2006 (the original developer was long gone, and I had never coded Java before), where I get this error:

EncryptionException: javax.crypto.IllegalBlockSizeException: input length must be a multiple of 8 when decrypting with advanced encryption

The code that he refers to is as follows:

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 am not entirely sure about the internal workings of the program, but I know that there are several configuration files and a key.properties file in this project directory. Regarding the "Input Length" (as indicated in the error message), my password for the database is 15 characters, and the "key" in key.properties is 25 characters. I have no idea if it matters or not.

Notes:

  • I tried to change the database password to 16 characters (a multiple of 8), but to no avail.
  • I read this and this and they didn’t help
  • I am moving this project from one server to another. It runs on its source server.
  • The source server runs JRE 1.4.2. The new server launches JRE 1.6u27.
  • I REALLY do not want to rebuild .jar. I am not a Java developer, and the project is quite massive.

Thank you for your help.

+1
source share
2 answers

The input to which the error message relates is an encryption text (strangely named cleartext ), the result of a Base-64 decoding operation. Make sure that the encryptedString you pass to this method is decoded into an array of bytes with a length multiple of 8.

+5
source

You probably shouldn't change the version of the JRE unless you want to review the code. I would try to lower the version of JRE on the new server first of all, especially since the code worked earlier.

+1
source

Source: https://habr.com/ru/post/949108/


All Articles