簡體   English   中英

區別 SHA256 C# 到 PHP

[英]difference SHA256 C# to PHP

我無法修改腳本 C#。 我擁有並制作 PHP 中的 2 個字符串,就像 C# 一樣。

但是這兩個 Hash 是不同的。 (UTF-8 兩側。)

我不知道 hash C# 與陣列?,你能幫我嗎?

C#

//Create a HMACSHA256 routine that uses the key bytes as the encryption key
        var temp = "/gggg";
        var strIntegratorKey = "1f09ae15-017a-4806-bcd6-825532123455";
        var timeStamp = "20210114124401";
        var utf8 = new System.Text.UTF8Encoding(false);
        var keyBytes = utf8.GetBytes(strIntegratorKey.ToString().ToLower());
        string signature;
        using (var hmacSha256 = new System.Security.Cryptography.HMACSHA256(keyBytes))
        {
            var signatureBytes = utf8.GetBytes(String.Concat(temp, timeStamp));
            signature = Convert.ToBase64String(hmacSha256.ComputeHash(signatureBytes));
        }
        Console.WriteLine(signature);

C# 確定:rdSay3xHElJh7xQj6l4LFYniVVVPVSwc46jpoXY+y8o=

php

$testKey="1f09ae15-017a-4806-bcd6-825532123455";
$testkey1 =utf8_encode(strtolower($testkey));
$bytes = "";
for($i = 0; $i < strlen($testkey1); $i++){
    $bytes.= ord($testkey1[$i]).";";
}
$keyToBytes=$bytes;
echo $keyToBytes."<br>";
//result
$keyToBytes = 49;102;48;57;97;101;49;53;45;48;49;55;97;45;52;56;48;54;45;98;99;100;54;45;56;50;53;53;51;50;49;50;51;52;53;53;

$exemple="/gggg20210114124401";
$text=utf8_encode($exemple);
$bytes = "";
for($i = 0; $i < strlen($text); $i++){
    $bytes.= ord($text[$i]).";";
}
$signatureBytes=$bytes;
echo $signatureBytes."<br>";
//result
$signatureBytes = 47;103;103;103;103;50;48;50;49;48;49;49;52;49;50;52;52;48;49;
$hm=hash_hmac("sha256",$signatureBytes,$keyToBytes ,true);
$hm64=base64_encode($hm);
echo $hm64."<br>";
``

PHP:brvlMd5OrT1BQlrdJKfo5qXJrx7ihpa3BIUn1x8w0us=

您正在無緣無故地構建一個形式為"digit;digit;digit;digit;..."的字符串,並將其用作hash_hmac()的輸入。 這意味着您為hash_hmac() function 提供與代碼的 C# 版本完全不同的輸入。 C#版本直接使用數據。

在不更改輸入的情況下,您也必須對 PHP 執行相同的操作。 代碼將如下所示:

$testKey="1f09ae15-017a-4806-bcd6-825532123455";
$message = '/gggg20210114124401';
$hm=hash_hmac("sha256", $message, $testKey,true);
$hm64=base64_encode($hm);

這將在$hm64

rdSay3xHElJh7xQj6l4LFYniVVVPVSwc46jpoXY+y8o=

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM