How to add PBKDF2WithHmacSHA1 for android api 8 (Froyo)

I need help. I am writing an application and should use the algorithm "PBKDF2WithHmacSHA1" (I can not change the server side). but this does not work in android (not supported), but then I create an android-maven project in eclipse, it works fine on Gingerbread and the newest one. but on Froyo not. I am trying to add a security provider that has this algorithm, but nothing. I can see it in the avaylable list, but I can not get an instance of it. I am an event try spoungycastle (recompiled bouncecastle), SunJCE. Maybe you have the same suggestion, how to fix it? o some kind of workaroud?

in this line i get an error

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

Error:

 W/System.err( 1225): java.security.NoSuchAlgorithmException: SecretKeyFactory PBKDF2WithHmacSHA1 implementation not found: W/System.err( 1225): at java.security.Provider$Service.newInstance(Provider.java:1100) W/System.err( 1225): at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:112) W/System.err( 1225): at javax.crypto.SecretKeyFactory.getInstance(SecretKeyFactory.java:111) W/System.err( 1225): at com.ii.app.AppActivity.onCreate(AppActivity.java:46) W/System.err( 1225): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) W/System.err( 1225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) W/System.err( 1225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) W/System.err( 1225): at android.app.ActivityThread.access$2300(ActivityThread.java:125) W/System.err( 1225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) W/System.err( 1225): at android.os.Handler.dispatchMessage(Handler.java:99) W/System.err( 1225): at android.os.Looper.loop(Looper.java:123) W/System.err( 1225): at android.app.ActivityThread.main(ActivityThread.java:4627) W/System.err( 1225): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 1225): at java.lang.reflect.Method.invoke(Method.java:521) W/System.err( 1225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) W/System.err( 1225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) W/System.err( 1225): at dalvik.system.NativeStart.main(Native Method) W/System.err( 1225): Caused by: java.lang.NullPointerException W/System.err( 1225): at com.sun.crypto.provider.SunJCE$2.run(DashoA13*..) W/System.err( 1225): at java.security.AccessController.doPrivilegedImpl(AccessController.java:264) W/System.err( 1225): at java.security.AccessController.doPrivileged(AccessController.java:84) W/System.err( 1225): at com.sun.crypto.provider.SunJCE.c(DashoA13*..) W/System.err( 1225): at com.sun.crypto.provider.SunJCE.b(DashoA13*..) W/System.err( 1225): at com.sun.crypto.provider.PBKDF2HmacSHA1Factory.<init>(DashoA13*..) W/System.err( 1225): at java.lang.Class.newInstanceImpl(Native Method) W/System.err( 1225): at java.lang.Class.newInstance(Class.java:1429) W/System.err( 1225): at java.security.Provider$Service.newInstance(Provider.java:1098) W/System.err( 1225): ... 16 more 
+4
source share
1 answer

Spongycastle works, but it's not straightforward. You should do something like this:

 private static byte[] decryptPBKDF2WithBC(char[] password, byte[] data, byte[] salt, byte[] iv) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password), salt,CryptographicToolBox.CRYPTO_ITERATIONS); KeyParameter params = (KeyParameter)generator.generateDerivedParameters(CryptographicToolBox.KEY_SIZE); byte[] endcoded = params.getKey(); SecretKey key = new SecretKeySpec(endcoded, "AES"); Cipher ciph = Cipher.getInstance("AES/CBC/PKCS5Padding"); ciph.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv); return ciph.doFinal(data); } 
0
source

All Articles