Note that there are some explanatory texts on larger screens.

plurals
  1. POPBKDF2 in Java with Bouncy Castle vs .NET Rfc2898DeriveBytes?
    primarykey
    data
    text
    <p>I have some C# code that generates a key using PBKDF2.</p> <pre><code>//byte[] salt = new RNGCryptoServiceProvider().GetBytes(salt); byte[] salt = new byte[] { 19, 3, 248, 189, 144, 42, 57, 23 }; // for testing byte[] bcKey = new Rfc2898DeriveBytes("mypassword", salt, 8192).GetBytes(32); </code></pre> <p>This works fine. I am trying to implement the same in Java with Bouncy Castle. Can't get it to work (the fact that Java lacks unsigned types makes it further annoying).</p> <pre><code>SecureRandom random = new SecureRandom(); byte[] salt = u2s(new int[] { 19, 3, 248, 189, 144, 42, 57, 23 }); //random.nextBytes(salt); PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(("BLK" + password).toCharArray()), salt, keyTransformationRounds); KeyParameter params = (KeyParameter)generator.generateDerivedParameters(keyLengthBits); byte[] bcKey = params.getKey(); int[] bcKeyU = s2u(bcKey); System.out.println(new String(Base64.encode(bcKey), "UTF-8")); // Helper functions because Java has no unsigned types // // EDIT: THESE FUNCTIONS ARE INCORRECT. // See my answer below for the correct versions. // static byte[] u2s(int[] unsignedArray) throws IOException { byte[] signedArray = new byte[unsignedArray.length]; for (int i = 0; i &lt; signedArray.length; i++) { if (unsignedArray[i] &lt; 0 || unsignedArray[i] &gt; 255) { throw new IOException("unsignedArray at " + i + " was not within the range 0 to 255."); } signedArray[i] = (byte)(unsignedArray[i] - 128); } return signedArray; } static int[] s2u(byte[] signedArray) { int[] unsignedArray = new int[signedArray.length]; for (int i = 0; i &lt; unsignedArray.length; i++) { unsignedArray[i] = (int)(signedArray[i] + 128); } return unsignedArray; } </code></pre> <p>The resultant bcKey byte arrays differ. What am I doing wrong? Am I going about handling the conversion from unsigned to signed properly or will that not work as I expect?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload