?
Solved

Unreachable exception  IllegalBlockSizeException

Posted on 2007-10-16
21
Medium Priority
?
346 Views
Last Modified: 2008-01-09
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 ?
0
Comment
Question by:bachra04
  • 12
  • 8
21 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20087610
>>I got the above exception once an invalid password is passed to my method.

Invalid in what way?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20087625
You need to read the plaintext into a buffer where buffer.length % 8 == 0
0
 
LVL 2

Author Comment

by:bachra04
ID: 20087708
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 20087746
>>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?
0
 
LVL 2

Author Comment

by:bachra04
ID: 20088444
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20088511
>>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
0
 
LVL 2

Author Comment

by:bachra04
ID: 20088672
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 ?






0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20088730
I need to see the exact code you're running. Please paste it into the window. That wouldn't compile
0
 
LVL 2

Author Comment

by:bachra04
ID: 20088782
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20088834
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());
        }
>>
0
 
LVL 2

Author Comment

by:bachra04
ID: 20088995
So what's the problem?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20089019
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();
0
 
LVL 2

Author Comment

by:bachra04
ID: 20089124
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
0
 
LVL 92

Expert Comment

by:objects
ID: 20089890
>> Failed to decrypt/encrypt: null

suggests you are getting a NullPointerException
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20091406
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;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20091409
>>  int temp = sz % 8;

Can be deleted
0
 
LVL 2

Author Comment

by:bachra04
ID: 20093380
here :
sz is not declared or do you mean passwd instead of sz?

Thanks,

B.T
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 20093445
Sorry - i messed that up a bit. Change

>>
 final byte[] convertedStringBytes = Base64Util.decode(passwd);
      byte[] decodedData = null;
      int temp = sz % 8;
      int padSize = (sz / 8) * 8;
      if (padSize != convertedStringBytes.length)
      {
>>

to

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

0
 
LVL 2

Author Comment

by:bachra04
ID: 20093525
You mean :

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

Expert Comment

by:CEHJ
ID: 20093555
Exactly
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20095332
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question