Android SecretKeyFactory problems. Implementation

2020-06-16 05:32发布

问题:

I'm trying to create an application for Android that uses encryption to save user information and I cannot figure out what I'm doing wrong. I'm trying to create an instance of the SecretKeyFactory using the "PBKDF2WithHmacSHA1" algorithm, but the application keeps on throwing exceptions at that point in the program (it doesn't matter if it's in the emulator or on real hardware).

Code:

SecretKeyFactory secretFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

Exception:

java.security.NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found ...

Here's the weird thing... if I take this code and compile it as a regular Java application, it works... no exceptions are thrown and I can create encrypted files (and decrypt them) without errors.

I have also tried entering other algorithms (e.g. AES, PBEWithHmacSHA1AndDESede, PBEWithMD5AndDES, etc.) and they all produce the same error/exception at that line in the code (when compiling for Android).

I have the latest version of Java installed (JDK 1.6.0.18) , all updates applied to Eclipse and plug-ins, and the latest version of the Android SDK. I'm also running Windows 7 64-bit.

Please help, I have not found an answer to this in two days of Internet searching. Thanks.

回答1:

It might just not be a supported algorithm or that naming of it on Android.

Have you looked around the javax.crypto classes? https://developer.android.com/reference/javax/crypto/EncryptedPrivateKeyInfo.html

Here is an example using a different algorithm if that helps. http://www.anddev.org/viewtopic.php?p=11737

btw, add a "from-irc" tag to this post to get a google response. http://android-developers.blogspot.com/2010/01/irc-offce-hours-update.html



回答2:

This means the android SDK doesn't have implementation for this algorithm. You have two options:

  • switch to another, supported algorithm (I can't find a reference, so try them manually)
  • provide your own algorithm implementation


回答3:

Is there any 3rd parts implementation that can be used on Android? Firefox Sync for example is using PBKDF2WithHmacSHA1, so for me switching to another algorithm is no option, since that is what I'm trying to decrypt.



回答4:

Some of the algorithms are not available for every Android API. In the official documentation of SecretKeyFactory, you can check which one is available depending on the API.

For example, PBKDF2withHmacSHA1 is available from API 10+.