I want to regenerate my IV and Key from the password and salt I have stored. In my C# program I do It in the following way :
//passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
The code for this is PHP returning is return a different IV and Key :
<?php
include("enctest_inc.php");
$password = "mypassword";
$salt = "mysalthereitis";
$hasher = "SHA256";
$iterations = 1000;
$keysize = 256;
$ivsize = 128;
$out = pbkdf2($hasher, $password, base64_decode($salt), $iterations, ($keysize+$ivsize)/8, true);
// split key and IV
$key = substr($out, 0, $keysize/8);
$iv = substr($out, $ivsize/8);
// print for demonstration purposes
echo base64_encode($key);
echo "<br>";
echo base64_encode($iv);
echo '<br>';
echo '<br>';
echo 'KEY : LNY893Wa00onNQh4ignFdjaVkg6GdxomMZoR/axB+Mw='; //KEY FROM SSH PROGRAM WHICH IS THE RIGHT RETURN;
echo '<br>';
echo 'IV : //QxQj7PDMRE1YUVo+mJvQ==' //IV FROM SSH PROGRAM WHICH IS THE RIGHT RETURN;
I really wonder where the problem lays or how to debug this ?
The settings are in both PHP and C# the same :
PHP CODE :
$hasher = "SHA256";
$iterations = 1000;
$keysize = 256;
$ivsize = 128;
C# CODE :
string random_string_pwd = "mypassword";
byte[] passwordBytes = Encoding.UTF8.GetBytes(random_string_pwd);
//var random_salt = new ClientTools();
//string random_string_salt = random_pwd.RndStrings(15);
string random_string_salt = "mysalthereitis";
byte[] Salt = Encoding.UTF8.GetBytes(random_string_salt);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);