Translating Java 256 bit password based encryption to C#

313 views Asked by At

I need to implement an API in C# that'll encrypt secrets identically to the API written in java.

The Java code looks like this

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password, salt, iteration, size);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "HmacSHA256");

My attempt on C# code looks like this

var secret = KeyDerivation.Pbkdf2(
    password, salt, KeyDerivationPrf.HMACSHA256, iteration, size);

When I compare the generated secrets, converted the to base 64, they're not the same. Their length are couple hundreds characters off as well.

What's missing from the C# code to generate the same secret as the Java API?

0

There are 0 answers