【问题标题】:HMACSHA1 equivalent in PHPPHP 中的 HMACSHA1 等效项
【发布时间】:2015-12-05 23:05:43
【问题描述】:

我正在尝试在 C# 和 PHP 中使用 HMACSHA1 进行加密,但我得到了不同的结果。 C#部分完成。我想让 PHP 部分生成与 C# 部分相同的结果。

C#

     string key = "x94IudsnSUWCDSiSxRU5qDSRs88=";
     string text = "The quick brown fox jumps over the lazy dog";

     HMACSHA1 hmac = new HMACSHA1();
     hmac.Key = Convert.FromBase64String(key);
     byte[] hashedData = hmac.ComputeHash(Encoding.UTF8.GetBytes(text));
     string hash = Convert.ToBase64String(hashedData, Base64FormattingOptions.None);
     Console.WriteLine(WebUtility.UrlEncode(hash));

PHP

    $key = "x94IudsnSUWCDSiSxRU5qDSRs88=";
    $text = "The quick brown fox jumps over the lazy dog";

    $hash = base64_encode(hash_hmac('sha1', $text, $key,true));
    echo urlencode($hash);

结果: C#:uAG0CDzyuEq7zbQ5ZfpVrb1ZUcA%3D PHP:hx2c5SS6xI%2B8hQBoUqsWQT4KwP4%3D

【问题讨论】:

    标签: c# php encryption output sha1


    【解决方案1】:

    您可能必须在 php 中对密钥进行 base64decode 才能将其传递给 hash_hmac 函数

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 2014-09-08
      • 2021-04-10
      • 1970-01-01
      相关资源
      最近更新 更多