ophio / secure-preferences

Android secure shared preferences using Android Keystore system
https://medium.com/@vashisthg/android-secure-shared-preferences-10f8356a4c2b
Apache License 2.0
348 stars 56 forks source link

IllegalStateException #18

Closed hearsilent closed 8 years ago

hearsilent commented 8 years ago

I init the KeyGenerator by this code:

keyGenerator = KeyStoreKeyGenerator.get(application, application.getPackageName());

But I got the IllegalStateException:

W/System.err: java.lang.IllegalStateException: java.security.InvalidKeyException: Failed to unwrap key
W/System.err:     at in.co.ophio.secure.core.KeyStoreKeyGenerator.<init>(KeyStoreKeyGenerator.java:51)
W/System.err:     at in.co.ophio.secure.core.KeyStoreKeyGenerator.get(KeyStoreKeyGenerator.java:56)

How can I resolve this problem?

vashisthg commented 8 years ago

Hey will look into this,

Can you tell us about the android version and the device used?

cc @ragdroid @vickyturtle

hearsilent commented 8 years ago

LG Nexus 5X Android 6.0.1

Thx :)

vashisthg commented 8 years ago

@hearsilent Please share the whole stacktrace.

Right now I think maybe the exception is related to key policy settings in your jdk.

hearsilent commented 8 years ago

Oops, the whole stacktrace has disappear. If I get the exception next time, I'll share the whole stacktrace for you.

Thanks for your help.

hearsilent commented 8 years ago

@vashisthg I got the exception again.

05-13 11:41:46.757 20434-20434/com.hearsilent.example W/System.err: Caused by: java.security.InvalidKeyException: Failed to unwrap key
05-13 11:41:46.758 20434-20434/com.hearsilent.example W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineUnwrap(AndroidKeyStoreCipherSpiBase.java:618)
05-13 11:41:46.758 20434-20434/com.hearsilent.example W/System.err:     at javax.crypto.Cipher.unwrap(Cipher.java:1707)
05-13 11:41:46.758 20434-20434/com.hearsilent.example W/System.err:     at in.co.ophio.secure.vault.SecretKeyWrapper.unwrap(SecretKeyWrapper.java:112)
05-13 11:41:46.759 20434-20434/com.hearsilent.example W/System.err:     at in.co.ophio.secure.core.KeyStoreKeyGenerator.loadOrGenerateKeys(KeyStoreKeyGenerator.java:78)
05-13 11:41:46.759 20434-20434/com.hearsilent.example W/System.err:     at in.co.ophio.secure.core.KeyStoreKeyGenerator.<init>(KeyStoreKeyGenerator.java:46)
05-13 11:41:46.760 20434-20434/com.hearsilent.example W/System.err:     ... 74 more
05-13 11:41:46.760 20434-20434/com.hearsilent.example W/System.err: Caused by: javax.crypto.IllegalBlockSizeException
05-13 11:41:46.761 20434-20434/com.hearsilent.example W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:486)
05-13 11:41:46.761 20434-20434/com.hearsilent.example W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineUnwrap(AndroidKeyStoreCipherSpiBase.java:616)
05-13 11:41:46.762 20434-20434/com.hearsilent.example W/System.err:     ... 78 more
05-13 11:41:46.762 20434-20434/com.hearsilent.example W/System.err: Caused by: android.security.KeyStoreException: Unknown error
05-13 11:41:46.763 20434-20434/com.hearsilent.example W/System.err:     at android.security.KeyStore.getKeyStoreException(KeyStore.java:632)
05-13 11:41:46.763 20434-20434/com.hearsilent.example W/System.err:     at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
05-13 11:41:46.764 20434-20434/com.hearsilent.example W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:473)
05-13 11:41:46.764 20434-20434/com.hearsilent.example W/System.err:     ... 79 more
vashisthg commented 8 years ago

@ragdroid @hearsilent I think the issue is due to key policy settings in the jdk as I too had it year back. A quick google search gives me http://stackoverflow.com/questions/6765240/rsa-aes-decryption-fails-invalidkeyexception .

adrientruong commented 7 years ago

Hi. I'm using this in an Android app and have users crashing on this problem. I read the Stackoverflow link you posted, however I am not 100% clear on what it means. Is there a fix for this issue or is it completely unavoidable?

marcinbak commented 6 years ago

@adrientruong @hearsilent @vashisthg I have exactly same issue (with same stack trace), that happens only on some Android devices... have you managed to find the reason of the problem or a way to fix it?

mjurekov commented 6 years ago

@vashisthg @ragdroid Any updates on this one?

@marcinbak / @adrientruong Have you maybe managed to solve the issue?

The same bug appears randomly on some Android devices in my app, however, after cleaning the app data the issue is gone. Could it be connected with writing/reading a key file from storage?

AlexanderKovalchuk commented 6 years ago

Could we reopen this ticket again? I have same issue in my app.

mjurekov commented 6 years ago

@AlexanderKovalchuk definitely. I'll try to reproduce it and will open another issue & PR once I'm sure where the problem is.

marcinbak commented 6 years ago

@mjurekov @vashisthg @ragdroid I have found a workaround for this issue. The cause is https://issuetracker.google.com/issues/37137351

Created pull request with the workaround https://github.com/ophio/secure-preferences/pull/26 but Travis seems not able to create emulator and failed check.

Tygara191 commented 6 years ago

I am also having this issue. Seems to be happening only when I minify my code, at least based on my observations. I am seeing it happen on my Xiaomi A1 with the latest /as of writing this comment/ Android Oreo update. No problems on my Xiaomi Redmi Note 4x, running MIUI9. That is just about all useful information I can provide.

ttenushko commented 5 years ago

Did anyone try to set "android:allowBackup="false" in manifest? This helped me