[英]PBKDF2WithHmacSHA512 throwing NoSuchAlgorithmException
I am writing an app in Eclipse for android.我正在 Eclipse 中为 android 编写一个应用程序。 I tried using PBKDF2WithHmacSHA1 at first to hash my passwords, which worked well.
我首先尝试使用 PBKDF2WithHmacSHA1 来散列我的密码,效果很好。 But because of the weaknesses in SHA1, I decided to upgrade it to PBKDF2WithHmacSHA512.
但是由于SHA1的弱点,我决定将其升级为PBKDF2WithHmacSHA512。 However, eclipse is now throwing a NoSuchAlgorithmException.
但是,eclipse 现在抛出 NoSuchAlgorithmException。
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512") throws NoSuchAlgorithmException SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512") 抛出 NoSuchAlgorithmException
I followed the instructions in the link above.我按照上面链接中的说明进行操作。 However, the java file linked in the question above requires the sun.crypto library, which I don't have on Eclipse.
但是,上面问题中链接的 java 文件需要 sun.crypto 库,而我在 Eclipse 上没有。 I was also advised not to use Sun library, but to use java official library.
还建议我不要使用 Sun 库,而是使用 java 官方库。
My question is, is there anyway of using PBKDF2WithHmacSHA512 on Eclipse?我的问题是,是否有在 Eclipse 上使用 PBKDF2WithHmacSHA512 的方法? Or if possible, can someone teach me how to break it into parts of doing the PBKDF2 first and then HmacSHA512 the result?
或者,如果可能的话,有人可以教我如何将其分解为先执行 PBKDF2 的部分,然后再执行 HmacSHA512 的结果吗?
Thank you.谢谢你。
Be careful when using SecretKeyFactory in android, please check supported API version, you will be got exception if using the wrong version like bellow: https://developer.android.com/reference/javax/crypto/SecretKeyFactory在 android 中使用 SecretKeyFactory 时要小心,请检查支持的 API 版本,如果使用错误的版本会出现异常,如下所示: https : //developer.android.com/reference/javax/crypto/SecretKeyFactory
This exception is thrown when a particular cryptographic algorithm is requested but is not available in the environment.当请求特定的加密算法但在环境中不可用时会引发此异常。 So based on the requirement above 👆 if you also want to use this algorithm on the low android version, I would recommend you use Bouncy Castle Library.
所以基于上面的需求👆,如果你也想在低安卓版本上使用这个算法,我会推荐你使用Bouncy Castle Library。
// Gradle
implementation 'org.bouncycastle:bcpkix-jdk15to18:1.70'
implementation 'org.bouncycastle:bcprov-jdk15to18:1.70'
// Use "PBKDF2WithHmacSHA512" with android O version and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512")
val pwSpec = PBEKeySpec(secret.toCharArray(), salt, iterations, keyLength)
skf.generateSecret(pwSpec).encoded
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.