相关疑难解决方法(0)

PHP和C#HMAC SHA256

我需要在C#中转换以下php代码:

$res = mac256($ent, $key);
$result = encodeBase64($res);
Run Code Online (Sandbox Code Playgroud)

哪里

function encodeBase64($data)
{
    $data = base64_encode($data);
    return $data;
}
Run Code Online (Sandbox Code Playgroud)

function mac256($ent,$key)
{
    $res = hash_hmac('sha256', $ent, $key, true);//(PHP 5 >= 5.1.2)
    return $res;
}
Run Code Online (Sandbox Code Playgroud)

我使用以下C#代码:

byte[] res = HashHMAC(ent, key);
string result = System.Convert.ToBase64String(res);
Run Code Online (Sandbox Code Playgroud)

哪里

public byte[] HashHMAC(string ent, byte[] key)
{
   byte[] toEncryptArray =System.Text.Encoding.GetEncoding(28591).GetBytes(ent);

   HMACSHA256 hash = new HMACSHA256(key);
   return hash.ComputeHash(toEncryptArray);
}
Run Code Online (Sandbox Code Playgroud)

这个链接提供完整的php源代码

我也在php中检查了这篇文章hmac_sha256和c#的区别

而这一个C#相当于PHP中的hash_hmac

但结果却不尽相同.

php c# sha256 hmac

2
推荐指数
1
解决办法
6126
查看次数

标签 统计

c# ×1

hmac ×1

php ×1

sha256 ×1