简体   繁体   中英

PBKDF2WithHmacSHA512 SecretKeyFactory not available

This is a really odd error. On two machines, the code is running perfectly. I just set this up on a brand new machine and this isn't working. I'm getting the following error when running the script;

java.lang.RuntimeException: java.security.NoSuchAlgorithmException: PBKDF2WithHmacSHA512 SecretKeyFactory not available

The line of code that is causing the error is;

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance( "PBKDF2WithHmacSHA512" );

Using JDK 1.7.0

The code is all compiling correctly, it is just failing at run time on this line. I have a feeling this is some how related to a JAR file that is different or a JDK that is slightly different, but after checking everything across the different machines, everything looks identical.

Thoughts?

Support for PBKDF2WithHmacSHA512 was added in Java 8, it is not available in Java 7 by default (Java 7 itself only supports PBKDF2WithHmacSHA1).

Compare:

  • SecretKeyFactory Algorithms for Java 7

    PBKDF2WithHmacSHA1 Constructs secret keys using the Password-Based Key Derivation Function function found in PKCS #5 v2.0.

  • SecretKeyFactory Algorithms for Java 8

    PBKDF2With<prf> Password-based key-derivation algorithm found in PKCS #5 2.0 using the specified pseudo-random function ( <prf> ). Example: PBKDF2WithHmacSHA256.

So you need to either upgrade to Java 8, or downgrade to PBKDF2WithHmacSHA1, or check if there is a JCE provider that provides PBKDF2WithHmacSHA512 for Java 7 (for example, Bouncy Castle).

If your code is running ok on another machine with Java 7, then check if lib/ext of that Java install contains additional libraries, for example Bouncy Castle has a JCE provider that supports PBKDF2WithHmacSHA512. In that case, you will need to include that same library in the Java install of the other machine.

I was getting the same error because I was running a unit test with PowerMockRunner , ie:

@RunWith(PowerMockRunner.class)
public class MyTest {
    //...
}

Removing @RunWith(PowerMockRunner.class) solved the issue for me.

If any Android developer came here with the same issue, continue reading.

I was having the same problem.

NoSuchAlgorithmException: SecretKeyFactory PBKDF2withHmacSHA256 implementation not found

I tried to upgrade from Java7 to Java8 but it didn't helped. Strange was that it was working on some devices and failing on the others. You'll think that SecretKeyFactory is in javax so it's JDK part and has nothing to do with Android API level.

Thing is that on Android system there are various security providers for different API levels and SecretKeyFactory provides implementations of algorithms from that providers, and based on the information here and here PBKDF2withHmacSHA512 is available from API 26 only. If you want to have functionality working on older Android systems you can switch to PBKDF2withHmacSHA1 which is available from API 10 or do/use some custom implementations of desired ones.

Other option is copy pasting algorithm's code from https://gist.github.com/jtan189/3804290 and put in your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM