Note that there are some explanatory texts on larger screens.

plurals
  1. POPBKDF2 with bouncycastle in Java
    primarykey
    data
    text
    <p>I'm trying to securely store a password in a database and for that I chose to store its hash generated using the PBKDF2 function. I want to do this using the bouncy castle library but I don't know why I cannot get it to work by using the JCE interface... The problem is that generating the hash in 3 different modes: <br> 1. using the PBKDF2WithHmacSHA1 secret key factory provided by sun <br> 2. using the bouncy castle api directly <br> 3. using the bouncy castle through JCE <br> results in 2 distinct values: one common to the first two and one for the third.</p> <p>Here is my code:</p> <pre><code> //Mode 1 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec keyspec = new PBEKeySpec("password".toCharArray(), salt, 1000, 128); Key key = factory.generateSecret(keyspec); System.out.println(key.getClass().getName()); System.out.println(Arrays.toString(key.getEncoded())); //Mode 2 PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(("password").toCharArray()), salt, 1000); KeyParameter params = (KeyParameter)generator.generateDerivedParameters(128); System.out.println(Arrays.toString(params.getKey())); //Mode 3 SecretKeyFactory factorybc = SecretKeyFactory.getInstance("PBEWITHHMACSHA1", "BC"); KeySpec keyspecbc = new PBEKeySpec("password".toCharArray(), salt, 1000, 128); Key keybc = factorybc.generateSecret(keyspecbc); System.out.println(keybc.getClass().getName()); System.out.println(Arrays.toString(keybc.getEncoded())); System.out.println(keybc.getAlgorithm()); </code></pre> <p>I know that PBKDF2 is implemented using HMAC SHA1 so that is why i chose as algorithm in the last method the "PBEWITHHMACSHA1" which i took from the bouncy castle java docs.</p> <p>The output is the following:</p> <pre><code>com.sun.crypto.provider.SunJCE_ae [-53, 29, 113, -110, -25, 76, 115, -127, -64, 74, -63, 102, 75, 81, -21, 74] [-53, 29, 113, -110, -25, 76, 115, -127, -64, 74, -63, 102, 75, 81, -21, 74] org.bouncycastle.jce.provider.JCEPBEKey [14, -47, -87, -16, -117, -31, 91, -121, 90, -68, -82, -31, -27, 5, -93, -67, 30, -34, -64, -40] PBEwithHmacSHA </code></pre> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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