繁体   English   中英

将C#中的Rfc2898DeriveBytes转换为PHP中的hash_pbkdf2

[英]Rfc2898DeriveBytes in C# to hash_pbkdf2 in PHP

我有使用C#解密的代码,我尝试将其移植到PHP,这是我所做的:

在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();

在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);   

结果:php中的$ key不等于AES.Key C#

在PHP中:数组([0] => 192 [1] => 203 [2] => 6 [3] ......等等,在C#中:[0] 134 [1] 7 [2] 145 [3] 54 [4] 49 .......等

我的代码有什么问题吗?

$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); 

结果:php中的$ IV不是C#中的等价键,为什么?

$decrypted = mcrypt_decrypt
  (
      MCRYPT_RIJNDAEL_256,
      $key,
      $bytesToBeDecryptedbinstring,
      MCRYPT_MODE_CBC,
      $IV
   );

echo "decryp:".$decrypted;     
?>

我希望有人为此提供解决方案

在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);  // ---> 256 / 8 = 32
AES.IV = key.GetBytes(AES.BlockSize / 8); // ---> 128 / 8 = 16
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();

在PHP中

$decryptedBytes = NULL;
$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]);
}

$AESKeyLength = 265/8;
$AESIVLength = 128/8;

$key = hash_pbkdf2("sha1", $passwordBytesstring, $saltBytesstring, 1000, $AESKeyLength + $AESIVLength, true); 

$aeskey = (  substr($key,0,$AESKeyLength) );
$aesiv =  (  substr($key,$AESKeyLength,$AESIVLength) );

$decrypted = mcrypt_decrypt
      (
          MCRYPT_RIJNDAEL_128,
          $aeskey,
          $bytesToBeDecryptedbinstring,
          MCRYPT_MODE_CBC,
          $aesiv
       );
$arr = str_split($decrypted);
for($i=0;$i<count($arr);$i++){
    $arr[$i] = ord($arr[$i]);
}
$decryptedBytes = $arr;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM