secure-preferences icon indicating copy to clipboard operation
secure-preferences copied to clipboard

NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found

Open albertovelazmoliner opened this issue 8 years ago • 20 comments

I'm getting this internal crash in all users with Samsung SCH-I705 (Samsung Tab2 7") with Adnroid 4.1.2, in the line what I'm doing this: sPrefs = new SecurePreferences(context);

I'm importing the library in gradle file: compile 'com.scottyab:secure-preferences-lib:0.1.3'

Fatal Exception: java.lang.RuntimeException: Unable to create application com.fieldaware.app.FAApplication: java.lang.IllegalStateException: java.security.NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found at android.app.ActivityThread.handleBindApplication(ActivityThread.java) at android.app.ActivityThread.access$1400(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at dalvik.system.NativeStart.main(NativeStart.java) Caused by java.lang.IllegalStateException: java.security.NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found at com.securepreferences.SecurePreferences.(Unknown Source) at com.securepreferences.SecurePreferences.(Unknown Source) at com.securepreferences.SecurePreferences.(Unknown Source) at com.fieldaware.app.utils.Settings.(Unknown Source) at com.fieldaware.app.FAApplication.onCreate(SourceFile) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java) at android.app.ActivityThread.handleBindApplication(ActivityThread.java) at android.app.ActivityThread.access$1400(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at dalvik.system.NativeStart.main(NativeStart.java) Caused by java.security.NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found at org.apache.harmony.security.fortress.Engine.notFound(Engine.java) at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java) at javax.crypto.SecretKeyFactory.getInstance(SecretKeyFactory.java) at com.tozny.crypto.android.AesCbcWithIntegrity.generateKeyFromPassword(SourceFile) at com.tozny.crypto.android.AesCbcWithIntegrity.generateKeyFromPassword(SourceFile) at com.securepreferences.SecurePreferences.generateAesKeyName(SourceFile) at com.securepreferences.SecurePreferences.(Unknown Source) at com.securepreferences.SecurePreferences.(Unknown Source) at com.securepreferences.SecurePreferences.(Unknown Source) at com.fieldaware.app.utils.Settings.(Unknown Source) at com.fieldaware.app.FAApplication.onCreate(SourceFile) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java) at android.app.ActivityThread.handleBindApplication(ActivityThread.java) at android.app.ActivityThread.access$1400(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at dalvik.system.NativeStart.main(NativeStart.java)

No issues in others devices. Is there some workaround to avoid this issue? Thanks

albertovelazmoliner avatar Oct 24 '15 13:10 albertovelazmoliner

I got the same issue on

  • Samsung Galaxy S III (gt-i9300)
  • Samsung GALAXY Tab 2 7.0 (gt-p3100)
  • Samsung Galaxy Note 2 (gt-n7100)`

running Android 4.1.1 or Android 4.1.2 with secure-pref version 0.1.2.

Caused by java.lang.IllegalStateException: java.security.NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found
       at com.securepreferences.SecurePreferences.(Unknown Source)
       at com.securepreferences.SecurePreferences.(Unknown Source)
       at com.securepreferences.SecurePreferences.(Unknown Source)
       at solda.io.android.util.PrefUtils.getDefaultPreference(Unknown Source)
       at solda.io.android.util.PrefUtils.getProductsInShippingCart(Unknown Source)
       at solda.io.android.ui.activity.DrawerActivity.restoreShoppingCart(Unknown Source)
       at solda.io.android.ui.activity.DrawerActivity.onCreate(Unknown Source)
       at android.app.Activity.performCreate(Activity.java:5184)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
       at android.app.ActivityThread.access$600(ActivityThread.java:140)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4898)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.security.NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found
       at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
       at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
       at javax.crypto.SecretKeyFactory.getInstance(SecretKeyFactory.java:108)
       at com.tozny.crypto.android.AesCbcWithIntegrity.keys(Unknown Source)
       at com.securepreferences.SecurePreferences.getSharedPreferenceFile(Unknown Source)
       at com.securepreferences.SecurePreferences.(Unknown Source)
       at com.securepreferences.SecurePreferences.(Unknown Source)
       at com.securepreferences.SecurePreferences.(Unknown Source)
       at solda.io.android.util.PrefUtils.getDefaultPreference(Unknown Source)
       at solda.io.android.util.PrefUtils.getProductsInShippingCart(Unknown Source)
       at solda.io.android.ui.activity.DrawerActivity.restoreShoppingCart(Unknown Source)
       at solda.io.android.ui.activity.DrawerActivity.onCreate(Unknown Source)
       at android.app.Activity.performCreate(Activity.java:5184)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
       at android.app.ActivityThread.access$600(ActivityThread.java:140)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4898)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
       at dalvik.system.NativeStart.main(NativeStart.java)

Is there anyway we can choose which implementation to use based on Android version?

cyberrob-zz avatar Dec 02 '15 07:12 cyberrob-zz

@scottyab is this related? https://github.com/tozny/java-aes-crypto/issues/9

cyberrob-zz avatar Dec 02 '15 07:12 cyberrob-zz

Hey @cyberrob it seems related yes, those devices don't seem to have that algorithm or the fallback algorithm ( a shame :( ). Open to PR's to solve.

scottyab avatar Dec 02 '15 11:12 scottyab

We could resolve this problem by adding https://rtyley.github.io/spongycastle/ lib. Is it a single solution? Could anyone suggest better solution?

gostik avatar Dec 15 '15 08:12 gostik

It seems like SpongyCastle would be a good solution. Who wants to put a PR in for it? :)

inktomi avatar Jan 04 '16 18:01 inktomi

Any work around ? I am unable to use spongycastle in android studio, what i will have to do after adding lib in my project. also what is PR :)

hammad-tariq avatar Feb 09 '16 12:02 hammad-tariq

@inktomi Which SpongyCastle dependencies did you include in your Android Studio gradle project?

All of them?

compile 'com.madgag.spongycastle:core:1.54.0.0'
compile 'com.madgag.spongycastle:prov:1.54.0.0'
compile 'com.madgag.spongycastle:pkix:1.54.0.0'
compile 'com.madgag.spongycastle:pg:1.54.0.0'

fernandospr avatar May 02 '16 15:05 fernandospr

Any update here? Having this issue with many samsung phones, any advice on how to fix?

brandoFS avatar Jun 16 '16 15:06 brandoFS

@brandoFS For now I've added the SpongyCastle dependency to build.gradle //SpongeCastle compile('com.madgag.spongycastle:core:1.54.0.0') compile('com.madgag.spongycastle:prov:1.54.0.0') And added it in the MyApplication class

static { Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1); }

It's not the solution but it works. Hope that helps you as a patch for now.

albertovelazmoliner avatar Jun 16 '16 16:06 albertovelazmoliner

@albertovelazmoliner thank you

brandoFS avatar Jun 16 '16 16:06 brandoFS

I got it more in 4.1.2 . who can help me ??

lgengsy avatar Jun 20 '16 03:06 lgengsy

@lgengsy please use @albertovelazmoliner solution

gostik avatar Jun 20 '16 19:06 gostik

@albertovelazmoliner added your spongycastle dependencies but we are still seeing the crash in our latest release. Any other idea?

brandoFS avatar Jun 20 '16 19:06 brandoFS

I want to rewrite this with other encryption。

lgengsy avatar Jun 21 '16 04:06 lgengsy

I got the same issue... even after adding spongycastle.

That's frustrating :-(

PasqualePuzio avatar Jul 20 '16 08:07 PasqualePuzio

Strange that some of you are seeing adding spongy fix this issue and some not. @PasqualePuzio & @brandoFS (or anyone else with this issue) could you paste the output of http://stackoverflow.com/a/7561104 to help debug this.

The solution should be to add spongy, but never put it past samsung to mess something up that every other device honors...

doridori avatar Aug 31 '16 13:08 doridori

Is there any solution already? I found that in the SecurePreferencesOld already is the mechanism to detect if algorithm is supported on the running platform. Why it is nt in the new one?

Ther was problem that when user migrated to different phone they lost the configuration. But I think that used algorithm should be stored in preferences item to keep the same when new one will be introduced in the library to backward compatibility.

tprochazka avatar Oct 12 '16 16:10 tprochazka

I am handling this like -

try {

                    AesCbcWithIntegrity.SecretKeys myKey = AesCbcWithIntegrity.generateKeyFromPassword(Build.SERIAL, AesCbcWithIntegrity.generateSalt(), 100);
                /*SecurePreferences.Editor editor = sharedPreferences.edit();
                editor.putString("realmkey", myKey.toString().substring(0, 64));
                editor.commit();*/
                    settings.edit().putString("rm", myKey.toString().substring(0, 64)).apply();

                } catch (NoSuchAlgorithmException e) {

                    AesCbcWithIntegrity.SecretKeys myKey = AesCbcWithIntegrity.generateKey();
                    if (myKey.toString().length() > 64) {
                        settings.edit().putString("rm", myKey.toString().substring(0, 64)).apply();
                    }
                }

am I doing it the right way?

Recently, got the error in Samsung 4.1.2 device

i-m-aman avatar Dec 02 '18 14:12 i-m-aman

@deathstroke007 Is above fix is work for Samsung 4.1.2 device?.

vinojvetha avatar Jan 23 '19 06:01 vinojvetha

I haven't tested it. But it will work out since i am catching the exception and using generatekey(). Please feel free to test and intimate me with the results.

i-m-aman avatar Jan 23 '19 12:01 i-m-aman