I have a C # code that generates a key using PBKDF2.
byte[] salt = new byte[] { 19, 3, 248, 189, 144, 42, 57, 23 };
byte[] bcKey = new Rfc2898DeriveBytes("mypassword", salt, 8192).GetBytes(32);
It works great. I am trying to implement the same in Java with Bouncy Castle. It is impossible to get it working (the fact that Java has no unsigned types makes it even more annoying).
SecureRandom random = new SecureRandom();
byte[] salt = u2s(new int[] { 19, 3, 248, 189, 144, 42, 57, 23 });
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"));
static byte[] u2s(int[] unsignedArray) throws IOException
{
byte[] signedArray = new byte[unsignedArray.length];
for (int i = 0; i < signedArray.length; i++)
{
if (unsignedArray[i] < 0 || unsignedArray[i] > 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 < unsignedArray.length; i++)
{
unsignedArray[i] = (int)(signedArray[i] + 128);
}
return unsignedArray;
}
The resulting bcKey bytes are different. What am I doing wrong? Am I going to process the conversion from unsigned to the correct subscription or will it not work the way I expect?
source
share