bachra04
asked on
Unreachable exception IllegalBlockSizeException
Error decoding data, IllegalBlockSizeException
javax.crypto.IllegalBlockS izeExcepti on: Input length must be multiple of 8 when
decrypting with padded cipher
at com.sun.crypto.provider.Su nJCE_h.b(D ashoA12275 )
at com.sun.crypto.provider.Su nJCE_h.b(D ashoA12275 )
at com.sun.crypto.provider.Bl owfishCiph er.engineD oFinal(Das hoA12275)
at javax.crypto.Cipher.doFina l(DashoA12 275)
at
net.ubiquity.common.securi ty.provide rs.Generic Provider.d ecode(Gene ricProvide r.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 ?
javax.crypto.IllegalBlockS
decrypting with padded cipher
at com.sun.crypto.provider.Su
at com.sun.crypto.provider.Su
at com.sun.crypto.provider.Bl
at javax.crypto.Cipher.doFina
at
net.ubiquity.common.securi
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 ?
You need to read the plaintext into a buffer where buffer.length % 8 == 0
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?
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?
I doubt it's much to do with the password. Is the condition i mentioned in my last posting met?
ASKER
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?
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
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
ASKER
This is how the code looks like:
Try
{
Base64Util.decode(passwd);
}
catch (exception e)
{
System.out.println("Except ion : 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.IllegalBlockS izeExcepti on: Input length must be multiple of 8 when
decrypting with padded cipher
at com.sun.crypto.provider.Su nJCE_h.b(D ashoA12275 )
at com.sun.crypto.provider.Su nJCE_h.b(D ashoA12275 )
at com.sun.crypto.provider.Bl owfishCiph er.engineD oFinal(Das hoA12275)
at javax.crypto.Cipher.doFina l(DashoA12 275)
at
net.ubiquity.common.securi ty.provide rs.Generic Provider.d ecode(Gene ricProvide r.java:151 )
at
>> Exception : cannot decode password
So how to get rid of the above trace ?
Try
{
Base64Util.decode(passwd);
}
catch (exception e)
{
System.out.println("Except
}
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.IllegalBlockS
decrypting with padded cipher
at com.sun.crypto.provider.Su
at com.sun.crypto.provider.Su
at com.sun.crypto.provider.Bl
at javax.crypto.Cipher.doFina
at
net.ubiquity.common.securi
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
ASKER
private String cryptPassword(String passwd, boolean decrypt)
{
// Obtain tool to use to decrypt/encrypt the properties file
USecurityProvider secProvider =
USecurityFactory.getInstan ce().getPr ovider(USe curityFact ory.BLOWFI SH_SECURIT Y);
//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(convert edStringBy tes);
// 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(encrypte dData);
// 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
{
// Obtain tool to use to decrypt/encrypt the properties file
USecurityProvider secProvider =
USecurityFactory.getInstan
//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(convert
// create the new decrypted string.
String finalvalue = new String(decodedData, "UTF8");
return finalvalue;
}
else
{
// Encrypt the bytes
final byte[] encryptedData = secProvider.encode(passwd.
// Encode the encrypted bytes
String encodedStr = Base64Util.encode(encrypte
// 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());
}
>>
>>
catch (Exception e)
{
// some exception handling code here
LOG.error("Failed to decrypt/encrypt: " + e.getMessage());
}
>>
ASKER
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();
>>LOG.error("Failed to decrypt/encrypt: " + e.getMessage());
can you post the result of
LOG.error("Failed to decrypt/encrypt");
e.printStackTrace();
ASKER
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
>> Failed to decrypt/encrypt: null
suggests you are getting a NullPointerException
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.lengt h)
{
byte[] padded = new byte[padSize + 8];
System.arraycopy(converted StringByte s, 0, padded, 0, convertedStringBytes.lengt h);
convertedStringBytes = null;
//Decrypt the bytes
decodedData = secProvider.decode(padded) ;
}
else
{
decodedData = secProvider.decode(convert edStringBy tes);
}
// create the new decrypted string.
String finalvalue = new String(decodedData, "UTF8");
return finalvalue;
}
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.lengt
{
byte[] padded = new byte[padSize + 8];
System.arraycopy(converted
convertedStringBytes = null;
//Decrypt the bytes
decodedData = secProvider.decode(padded)
}
else
{
decodedData = secProvider.decode(convert
}
// create the new decrypted string.
String finalvalue = new String(decodedData, "UTF8");
return finalvalue;
}
>> int temp = sz % 8;
Can be deleted
Can be deleted
ASKER
here :
sz is not declared or do you mean passwd instead of sz?
Thanks,
B.T
sz is not declared or do you mean passwd instead of sz?
Thanks,
B.T
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You mean :
final byte[] convertedStringBytes = Base64Util.decode(passwd);
byte[] decodedData = null;
int padSize = (convertedStringBytes.leng th / 8) * 8;
if (padSize != convertedStringBytes.lengt h)
{
final byte[] convertedStringBytes = Base64Util.decode(passwd);
byte[] decodedData = null;
int padSize = (convertedStringBytes.leng
if (padSize != convertedStringBytes.lengt
{
Exactly
:-)
Invalid in what way?