1

I want to convert my c# codes to java with PBKDF2 hashing and with the same result(no real product, just test).

C#:

static string Pbkdf2Hashing(string password)
    {
        byte[] salt = new byte[128 / 8];
        string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
            password: password,
            salt: salt,
            prf: KeyDerivationPrf.HMACSHA1,
            iterationCount: 10000,
            numBytesRequested: 256 / 8));
        return hashed;
    }

The result:

  1. List item

    oudaCubzWVIMjTxaQh1KT85fn+p2KjQRdBDXpiS8AUA=

Java:

 static String Pbkdf2Hashing(String password) throws Exception {
    byte[] salt = new byte[128 / 8];
    int iterations = 10000;
    int derivedKeyLength = 256;
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);
    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    byte[] result = f.generateSecret(spec).getEncoded();
    return Base64.getEncoder().encodeToString(result);
}

The result:

dxtD2eQ/4Sj5pGnPqCTiuL6jns5apO1OHkTaJC9DTzw=

1 Answer 1

2

So that the Java code gives the same result as the C# code, simply replace PBKDF2WithHmacSHA256 with PBKDF2WithHmacSHA1 in the Java code.

Since you didn't post the plaintext to your example, I use for my test the plaintext

The quick brown fox jumps over the lazy dog

for which the C# Code and the fixed Java Code both return

mPfEIpaydCQU15ACyPW+jPh/ctqi8q74aWhO9nWz9Q0=

as result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.