Android Addicted Apps Creator

February 12, 2013

javax.crypto.BadPaddingException: pad block corrupted

Filed under: Android — alin b. @ 12:21


Now, how horror does this error sound to you ? Encryption is already a very complex process and after a while when it just throws this error… well, is not very nice. This happened to me, after one of my app stopped working. If worked fine since android 2.1, but now I was getting feedback from users which ran Android 4.2 that the app has stopped responding.

Did a bit of search on and found out the culprit

Cryptography — Modified the default implementations of SecureRandom and Cipher.RSA to use OpenSSL. Added SSLSocket support for TLSv1.1 and TLSv1.2 using OpenSSL 1.0.1

Went in my code and found out I use using SecureRandom and Cipher:

SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");

If we do a little test to see what provider does rand

get (rand.getProvider().getName()) 

we see:

  • android 4.2: AndroidOpenSSL
  • android < 4.2: Crypto

It is pretty clear that things are not ok. How do we fix this ? Actually very simple. If we look at the docs we can see that SecureRandom.getInstance() can also be called with provider parameter. 

public static SecureRandom getInstance (String algorithm, Provider provider)

So, to make our code work, we simply update it to:

SecureRandom rand = SecureRandom.getInstance( "SHA1PRNG", "Crypto" )

More info at

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: