I have a Java class that’s been failing for years now. It’s got some very bad padding in it that prevents it from working correctly. A lot of people have been asking about it, but no one seems to be really able to help, so I thought I’d share my findings.
The bug is called “crypto badpaddingexception”. Apparently, the class that has the problem is called javax.crypto.BadPaddingException. It’s very easy to test, you just need to create a bad-padding-string and call the getBytes( ) method. You will get an exception saying “An internal error occurred”.
The string you used to create your bad-padding string is the standard java.lang.String constructor. But the string is actually an “empty” string, so the getBytes method actually throws an exception if the string is not an array. So in the above example, the getBytes method is throwing the exception because null is not an array, and the String constructor throws an exception because the string is empty.
The problem is that this is a common error because there are so many strings that are not arrays. As such, you should always check for null before creating an array. You’ll also need to check for the length of the array too. If you created the array with new String myArray, it will have a length of 0. If you did it earlier then it will still have a length of 0. And you can also check to see if the length is greater than zero.
And the same goes for arrays. The only time you can ever use a “null” value is when creating an array from a null object, like the null object in the String constructor. The String constructor will throw an exception if you try to use a “null” value.
crypto badpaddingexception problem is because it’s trying to use a null object. Because the string object doesn’t have a length field it can’t be used for padding, so it throws an exception which ends up being something like “JAXB exception: BadPadding”.