我有在C#中解密的代码,我尝试将其移植到PHP,这就是我所做的:
在C#中
<div class="row">
<div class="col-md-6 blue1">
1
<div class="row">
<div class="col-md-6 blue2">2</div>
<div class="col-md-6 blue3">3</div>
</div>
<div class="row">
<div class="col-md-2 blue4">4</div>
<div class="col-md-10 blue5">5</div>
</div>
</div>
<div class="col-md-4 blue6">
img
</div>
<div class="col-md-2 blue7">
x
</div>
PHP中的
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中的$ key不等于AES.Key C#
PHP中的:数组([0] =&gt; 192 [1] =&gt; 203 [2] =&gt; 6 [3] ......等等 在C#:[0] 134 [1] 7 [2] 145 [3] 54 [4] 49 .......依此类推
我的代码有问题吗?
$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中的$ IV不是C#中的等号键,为什么?
$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);
我希望有人能为此付出代价。
答案 0 :(得分:1)
在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;