Android developers! Java.security

If you are having a problem trying to run some code for encryption and keep getting the error javax.crypto.badPadding error then you have to change the how you use the security object from the package java.security;

This error only occurs for API 4.2 but any other below you are good to go.

Here is the code

The following code will produce two different outputs on pre-Android 4.2 and Android 4.2:

SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
Log.i(TAG, "rand.getProvider(): " + rand.getProvider().getName());

On pre-4.2 devices:

rand.getProvider: Crypto

On 4.2 devices:

rand.getProvider: AndroidOpenSSL

SO what happens is that every time you run it, it generates a new rawKey everytime you run it.

Leave a comment