Link to home
Start Free TrialLog in
Avatar of bachra04
bachra04Flag for Canada

asked on

Unreachable exception IllegalBlockSizeException

Error decoding data, IllegalBlockSizeException
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when
decrypting with padded cipher
        at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
        at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
        at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA12275)
        at javax.crypto.Cipher.doFinal(DashoA12275)
        at
net.ubiquity.common.security.providers.GenericProvider.decode(GenericProvider.java:151)
        at

I got the above exception once an invalid password is passed to my method.

The problem I had is that I want to catch this exception in order to output a customized message but I couldnt

When using the following code the exception block is reached but I still get the trace.

Try
{

}
Catch (Exception)
{

}

When using the following code the exception block is never reached :
Try
{

}
Catch (IllegalBlockSizeException)
{

}


Any help to catch this exception ?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>I got the above exception once an invalid password is passed to my method.

Invalid in what way?
You need to read the plaintext into a buffer where buffer.length % 8 == 0
Avatar of bachra04

ASKER

invalid in the sense that a user can enter a password that is not encrypted by the application so that when decrypting it I got the following error.
I know why I got the error.
But I coudn't handle the exception for some reason.
So what is this reason?
>>So what is this reason?

I doubt it's much to do with the password. Is the condition i mentioned in my last posting met?
even if I pass a password with a multiple of 8 I usually have the same printtrace message.
Again how can I catch this exception and why it does not enter the exception bock?
>>even if I pass a password with a multiple of 8 I usually have the same printtrace message.

Yes, because, as i said, it's probably nothing to do with your password, but to do with the buffer size used to encrypt

>>Again how can I catch this exception and why it does not enter the exception bock?

What are you going to do when you do catch it actually? The reason you aren't catching it is probably because the method that throws it is not being called in the right try..catch block
This is how the code looks like:
Try
{
 Base64Util.decode(passwd);
}
catch (exception e)
{
System.out.println("Exception :  cannot decode passwd");
}

When running the above program with invalid password I should get  the following output:

>> Exception : cannot decode password

But I got:

Error decoding data, IllegalBlockSizeException
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when
decrypting with padded cipher
        at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
        at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
        at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA12275)
        at javax.crypto.Cipher.doFinal(DashoA12275)
        at
net.ubiquity.common.security.providers.GenericProvider.decode(GenericProvider.java:151)
        at
>> Exception : cannot decode password

So how to get rid of the above trace ?






I need to see the exact code you're running. Please paste it into the window. That wouldn't compile
private String cryptPassword(String passwd, boolean decrypt)
    {
        // Obtain tool to use to decrypt/encrypt the properties file
        USecurityProvider secProvider =
           USecurityFactory.getInstance().getProvider(USecurityFactory.BLOWFISH_SECURITY);

        //The value was an encrypted value, so now decrypt the value.
        try
        {
            if (decrypt)
            {
                //Decode the bytes.
                final byte[] convertedStringBytes =
                        Base64Util.decode(passwd);

                //Decrypt the bytes
                final byte[] decodedData =
                        secProvider.decode(convertedStringBytes);

                // create the new decrypted string.
                String finalvalue = new String(decodedData, "UTF8");

                return finalvalue;
            }
            else
            {
                // Encrypt the bytes
                final byte[] encryptedData = secProvider.encode(passwd.getBytes());

                // Encode the encrypted bytes
                String encodedStr = Base64Util.encode(encryptedData);

                // Return encrypted password.
                return encodedStr;
            }
        }
        catch (Exception e)
        {
            // some exception handling code here
            LOG.error("Failed to decrypt/encrypt: " + e.getMessage());
        }

        return passwd;
    }
This is my method
OK. That looks quite different. You have one catch block only there, namely

>>
catch (Exception e)
        {
            // some exception handling code here
            LOG.error("Failed to decrypt/encrypt: " + e.getMessage());
        }
>>
So what's the problem?
Well, firstly, instead of

>>LOG.error("Failed to decrypt/encrypt: " + e.getMessage());

can you post the result of

LOG.error("Failed to decrypt/encrypt");
e.printStackTrace();
I will do it in a few hours since I have to leave now but just to let you know that the result is exactely:
>> Failed to decrypt/encrypt: null
>> Failed to decrypt/encrypt: null

suggests you are getting a NullPointerException
Try this approach. You might need to do something similar with encryption too:

if (decrypt)
{
      //Decode the bytes.
      final byte[] convertedStringBytes = Base64Util.decode(passwd);
      byte[] decodedData = null;
      int temp = sz % 8;
      int padSize = (sz / 8) * 8;
      if (padSize != convertedStringBytes.length)
      {
            byte[] padded = new byte[padSize + 8];
            System.arraycopy(convertedStringBytes, 0, padded, 0, convertedStringBytes.length);
            convertedStringBytes = null;
            //Decrypt the bytes
            decodedData = secProvider.decode(padded);
      }
      else
      {
            decodedData = secProvider.decode(convertedStringBytes);
      }      

      // create the new decrypted string.
      String finalvalue = new String(decodedData, "UTF8");

      return finalvalue;
}
>>  int temp = sz % 8;

Can be deleted
here :
sz is not declared or do you mean passwd instead of sz?

Thanks,

B.T
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You mean :

final byte[] convertedStringBytes = Base64Util.decode(passwd);
byte[] decodedData = null;
int padSize = (convertedStringBytes.length / 8) * 8;
if (padSize != convertedStringBytes.length)
 {
Exactly
:-)