I also use encryption in my apps and encountered various problems on 
different devices. Unfortunately the available crypto library 
implementation which comes with Android is apparently not really 
standardized and there are differences between manufacturers and devices. I 
even had a situation where a modern encryption method was available on my 
old Samsung Galaxy S running Android 2.2. But the same method was not 
available on other devices or later versions of Android.

You can get around that problem by bundling your own crypto implementation. 
Look for Bouncy Castle or the Android specific counterpart Spongy Castle. 
They are huge  and it's difficult to strip out only the components you need 
especially when it comes to finding the right Proguard configuration. But I 
think it's the safest you can do and you don't need to fear that your app 
might break on some other device or future version of Android.

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

Reply via email to