I have code to decryp in C# and I try to porting it to PHP, here what I have done:
in C#
byte[] decryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
in PHP
$saltBytes = array(1,2,3,4,5,6,7,8);
$saltBytesstring = "";
for($i=0;$i<count($saltBytes);$i++){ echo $i;
$saltBytesstring=$saltBytesstring+chr($saltBytes[$i]);
}
$key = hash_pbkdf2("sha1", $passwordBytesstring, $saltBytesstring, 1000, 32, true);
$arr1 = str_split($key);
for($i=0;$i<count($arr1);$i++){
$arr1[$i] = ord($arr1[$i]);
}
echo "\nKey:"; print_r($arr1);
Result: $key in php is not equal AES.Key C#
in PHP: Array ( [0] => 192 [1] => 203 [2] => 6 [3] ...... and so on in C#: [0]134 [1]7 [2]145 [3]54 [4]49 ....... and so on
do somethening wrong with my code?
$IV = hash_pbkdf2("sha1", $passwordBytesstring, $saltBytesstring, 1000,16,true);
$arr2 = str_split($IV);
for($i=0;$i<count($arr2);$i++){
$arr2[$i] = ord($arr2[$i]);
}
echo "\nIV:"; print_r($arr2);
Result: $IV in php is not equal key in C#, why?
$decrypted = mcrypt_decrypt
(
MCRYPT_RIJNDAEL_256,
$key,
$bytesToBeDecryptedbinstring,
MCRYPT_MODE_CBC,
$IV
);
echo "decryp:".$decrypted;
?>
I hope somebody give my sulution for this