Hi,
The exception message is misleading.
If you've encoded the passwords with an earlier version of the Cipher
library, then try to decode the password with a later version of the Cipher
library, an exception is raised (like the "padding" exception you've
posted).
I've run into this when switching versions of Android.
In your case, I recommend replacing the encrypted values with values
encrypted with new Cipher library version.
To summarize, the versions of the Cipher library probably don't match.
Thus, the "padding" algorithm don't match. Re-encode your values with new
Cipher library version. Using the same Cipher library version for both
encode and decode, should solve your issue:)
Hope that helps. Please post a reply if that solves your issue :)
Good Luck.
Jason
On Monday, January 7, 2013 5:18:29 AM UTC-6, David Asta wrote:
>
> I have this piece of code that I use to encrypt/decrypt password that are
> stored on a sqlite3 database. Up until now, I've been using my app on my
> HTC Desire (Android 2.2.2) without any problems. Recently I got a Nexus 7
> and to my surprise, my app didn't work. When trying to decrypt the
> passwords, all the passwords come as "error", which is what I return when a
> Exception occurs.
>
> I decided to debug it on Eclipse, and I discovered that my code only fails
> on Android 4.x. Works fine with 2.2 and 3.0. How is it possible that the
> same code doesn't work? Has really javax.crypto changed on Android 4.x?
>
> Thanks a lot in advance for any help.
>
>
> The error from LogCat:
>
>> 01-04 16:05:48.419: W/System.err(1452): javax.crypto.BadPaddingException:
>> pad block corrupted
>> 01-04 16:05:48.419: W/System.err(1452): at
>> com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:709)
>> 01-04 16:05:48.419: W/System.err(1452): at
>> javax.crypto.Cipher.doFinal(Cipher.java:1111)
>> 01-04 16:05:48.419: W/System.err(1452): at
>> info.datavase.passkeep.Crypto.decrypt(Crypto.java:60)
>> 01-04 16:05:48.419: W/System.err(1452): at
>> info.datavase.passkeep.Crypto.decrypt(Crypto.java:28)
>> 01-04 16:05:48.419: W/System.err(1452): at
>> info.datavase.passkeep.ViewSite.unlockPassword(ViewSite.java:59)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> java.lang.reflect.Method.invokeNative(Native Method)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> java.lang.reflect.Method.invoke(Method.java:511)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> android.view.View$1.onClick(View.java:3592)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> android.view.View.performClick(View.java:4202)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> android.view.View$PerformClick.run(View.java:17340)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> android.os.Handler.handleCallback(Handler.java:725)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> android.os.Handler.dispatchMessage(Handler.java:92)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> android.os.Looper.loop(Looper.java:137)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> android.app.ActivityThread.main(ActivityThread.java:5039)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> java.lang.reflect.Method.invokeNative(Native Method)
>> 01-04 16:05:48.439: W/System.err(1452): at
>> java.lang.reflect.Method.invoke(Method.java:511)
>> 01-04 16:05:48.449: W/System.err(1452): at
>> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
>> 01-04 16:05:48.449: W/System.err(1452): at
>> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
>> 01-04 16:05:48.449: W/System.err(1452): at
>> dalvik.system.NativeStart.main(Native Method)
>
>
>
> My class:
>
> package info.datavase.passkeep;
> import java.security.SecureRandom;
> import javax.crypto.Cipher;
> import javax.crypto.KeyGenerator;
> import javax.crypto.SecretKey;
> import javax.crypto.spec.SecretKeySpec;
> import android.util.Base64;
> public class Crypto {
> public static String encrypt(String seed, String cleartext) {
> try {
> byte[] rawKey = getRawKey(seed.getBytes());
> byte[] result = encrypt(rawKey, cleartext.getBytes());
> return Base64.encodeToString(result, Base64.NO_WRAP);
> }
> catch(Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> return "error";
> }
> public static String decrypt(String seed, String encrypted) {
> try {
> byte[] rawKey = getRawKey(seed.getBytes());
> byte[] enc = Base64.decode(encrypted, Base64.NO_WRAP);
> byte[] result = decrypt(rawKey, enc);
> return new String(result);
> }
> catch(Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> return "error";
> }
> private static byte[] getRawKey(byte[] seed) throws Exception {
> KeyGenerator kgen = KeyGenerator.getInstance("AES");
> SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
> sr.setSeed(seed);
> kgen.init(128, sr);
> SecretKey skey = kgen.generateKey();
> byte[] raw = skey.getEncoded();
> return raw;
> }
> private static byte[] encrypt(byte[] raw, byte[] clear) throws
> Exception {
> SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
> Cipher cipher = Cipher.getInstance("AES");
> cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
> byte[] encrypted = cipher.doFinal(clear);
> return encrypted;
> }
> private static byte[] decrypt(byte[] raw, byte[] encrypted) throws
> Exception {
> SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
> Cipher cipher = Cipher.getInstance("AES");
> cipher.init(Cipher.DECRYPT_MODE, skeySpec);
> byte[] decrypted = cipher.doFinal(encrypted);
> return decrypted;
> }
> }
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en