C#HMACSHA256给出的结果与PHP哈希_hmac不同

C#HMACSHA256给出的结果与PHP哈希_hmac不同,c#,php,.net,security,sha,C#,Php,.net,Security,Sha,我有以下php代码: //输出6975F8F502E59722A6D8760CC558E7867F36A312A5D336C4BA983DCFB81691 //在c语言中# //输出:26FFE2E29513304F96D44CB69210657B4E44E837B7C8E8947C667B344594F18 演示 我需要修改我的c代码以匹配php生成的值 更新:我尝试了在线sha生成器,它给出了我的c#结果经过多次尝试,我发现PHP包('H*',$hashSecret)使结果不同,因此我添加

我有以下php代码:

//输出6975F8F502E59722A6D8760CC558E7867F36A312A5D336C4BA983DCFB81691 //在c语言中#

//输出:26FFE2E29513304F96D44CB69210657B4E44E837B7C8E8947C667B344594F18 演示 我需要修改我的c代码以匹配php生成的值


更新:我尝试了在线sha生成器,它给出了我的c#结果

经过多次尝试,我发现PHP包('H*',$hashSecret)使结果不同,因此我添加了以下方法:

public static byte[] Pack(string key)
{
    key = key.Replace("-", "");
    byte[] raw = new byte[key.Length / 2];
    for (int i = 0; i < raw.Length; i++)
    {
        raw[i] = Convert.ToByte(key.Substring(i * 2, 2), 16);
    }
    return raw;
}
公共静态字节[]包(字符串键)
{
键=键。替换(“-”,“”);
字节[]原始=新字节[key.Length/2];
for(int i=0;i
您是否比较了正在使用的字节数组(用于表示数据和密钥)?不,我不知道PHP您可能会发现这很有用:
public static void Main()
{
    Console.Write(CreateToken("cardCVC=123&cardExp=2105&cardNum=5123456789012346&holderName=John Doe&mobileNumber=20100000000000","C0DF9A7B3819968807A9D4E48D0E65C6"));
}

 private static string CreateToken(string message, string secret)
{
  var encoding = new System.Text.UTF8Encoding();
  byte[] keyByte = encoding.GetBytes(secret);
  byte[] messageBytes = encoding.GetBytes(message);
  using (var hmacsha256 = new HMACSHA256(keyByte))
  {
    byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
    return BitConverter.ToString(hashmessage).Replace("-","");
  }
}
public static byte[] Pack(string key)
{
    key = key.Replace("-", "");
    byte[] raw = new byte[key.Length / 2];
    for (int i = 0; i < raw.Length; i++)
    {
        raw[i] = Convert.ToByte(key.Substring(i * 2, 2), 16);
    }
    return raw;
}