Reputation: 2832
I'm trying to convert the following code from C# to Java, but I get different result values of keys. Here are my codes:
C#:
plainText = "Hello World";
passPhrase = "IhDyHz6bgQyS0Ff1/1s=";
saltValue = "0A0Qvv09OXd3GsYHVrA=";
hashAlgorithm = "SHA1";
passwordIterations = 3;
initVector = "GjrlRZ6INgNckBqv";
keySize = 256;
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
Java
byte[] password = PassPhrase.getBytes("ASCII");
byte[] salt = PKCS5S1ParametersGenerator.PKCS5PasswordToBytes(SaltValue.toCharArray());
PKCS5S1ParametersGenerator generator = new PasswordDeriveBytes(new SHA1Digest());
generator.init(password, salt, PasswordIterations);
byte[] key = ((KeyParameter)generator.generateDerivedParameters(32)).getKey();
The issue is that I get different keys even with the same parameters provided on both codes: (only the first 4 bytes match)
NOTE: this values had been converted to Base64:
C#: GWR/loJAuuiPvP0cuGGCVXErz16HjOZ7yQkCCHfBux4= Java: GWR/4oCT
If I keep playing with the number provided to generateDerivedParameters(32), it only gives me more bytes but still doesn't match the result at .Net.
I can't change the .Net code, so please any suggestion must be done at Java side.
Thanks in advance.
Upvotes: 0
Views: 1629
Reputation: 28951
The generateDerivedParameters
method says: "keySize the size of the key we want (in bits)". So you have to use generateDerivedParameters(32 * 8)
Upvotes: 1