我需要一些帮助。我正在编写应用程序,需要使用" PBKDF2WithHmacSHA1"算法(不能改变服务器端)。但是那不能在android(不支持)中工作,但后来我在eclipse中创建了android-maven项目,它可以在Gingerbread和最新版本上正常工作。但是对于Froyo不要。我尝试添加具有该算法的安全提供程序,但没有。我可以在avaylable列表中看到它,但无法获得它的实例。我的事件尝试spoungycastle(重新编译bouncecastle),SunJCE。也许你有同样的建议如何解决它?某些workaroud?
在这一行我得到错误
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
错误:
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
答案 0 :(得分:0)
Spongycastle有效,但不是直截了当的。你必须做这样的事情:
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);
}