gpt4 book ai didi

c# Base64编码解码错误结果

转载 作者:行者123 更新时间:2023-11-30 23:19:45 27 4
gpt4 key购买 nike

我需要在 C# 中创建哈希签名。

我需要在我的 C# 代码中实现的伪代码示例:

Signatur(Request) = new String(encodeBase64URLCompatible(HMAC-SHA-256(getBytes(Z, "UTF-8"), decodeBase64URLCompatible(getBytes(S, "UTF-8")))), "UTF-8")

Z: apiSecretS: stringToSign

expectedSignaturapiSecret 的编码是Base64 URL Encoding [RFC 4648 Section 5]

我的问题是我总是得到错误的结果。

public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
return Encoding.UTF8.GetString(base64EncodedBytes);
}

public static string Base64Encode(string plainText)
{
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}

private static byte[] HmacSha256(string data, string key)
{
using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)))
{
return hmac.ComputeHash(Encoding.UTF8.GetBytes(data));
}
}

static void Main(string[] args)
{
var apiSecret = "JrXRHCnUegQJAYSJ5J6OvEuOUOpy2q2-MHPoH_IECRY=";
var stringToSign = "f3fea5f3-60af-496f-ac3e-dbb10924e87a:20160201094942:e81d298b-60dd-4f46-9ec9-1dbc72f5b5df:Qg5f0Q3ly1Cwh5M9zcw57jwHI_HPoKbjdHLurXGpPg0yazdC6OWPpwnYi22bnB6S";
var expectedSignatur = "ps9MooGiTeTXIkPkUWbHG4rlF3wuTJuZ9qcMe-Y41xE=";

apiSecret = apiSecret.Replace('-', '+').Replace('_', '/').PadRight(apiSecret.Length + (4 - apiSecret.Length % 4) % 4, '=');

var secretBase64Decoded = Base64Decode(apiSecret);

var hmac = Convert.ToBase64String(HmacSha256(secretBase64Decoded, stringToSign));

var signatur = hmac.Replace('+', '-').Replace('/', '_');


Console.WriteLine($"signatur: {signatur}");
Console.WriteLine($"expected: {expectedSignatur}");

Console.WriteLine(signatur.Equals(expectedSignatur));
Console.ReadLine();
}

最佳答案

您假设您的 key 最初是使用 UTF-8 编码的文本 - 但看起来它不是。您应该在逻辑上将二进制数据保持为二进制数据——您根本不需要Base64EncodeBase64Decode 方法。相反,您的 HmacSha256 方法应该采用 byte[] 作为键,您可以只使用 Convert.FromBase64String 从中获取这些字节base64 编码的 secret :

using System;
using System.Text;
using System.Security.Cryptography;

class Test
{
private static byte[] HmacSha256(byte[] key, string data)
{
using (var hmac = new HMACSHA256(key))
{
return hmac.ComputeHash(Encoding.UTF8.GetBytes(data));
}
}

static void Main(string[] args)
{
var apiSecret = "JrXRHCnUegQJAYSJ5J6OvEuOUOpy2q2-MHPoH_IECRY=";
var stringToSign = "f3fea5f3-60af-496f-ac3e-dbb10924e87a:20160201094942:e81d298b-60dd-4f46-9ec9-1dbc72f5b5df:Qg5f0Q3ly1Cwh5M9zcw57jwHI_HPoKbjdHLurXGpPg0yazdC6OWPpwnYi22bnB6S";
var expectedSignatur = "ps9MooGiTeTXIkPkUWbHG4rlF3wuTJuZ9qcMe-Y41xE=";

apiSecret = apiSecret.Replace('-', '+').Replace('_', '/').PadRight(apiSecret.Length + (4 - apiSecret.Length % 4) % 4, '=');

var secretBase64Decoded = Convert.FromBase64String(apiSecret);

var hmac = Convert.ToBase64String(HmacSha256(secretBase64Decoded, stringToSign));

var signatur = hmac.Replace('+', '-').Replace('/', '_');
Console.WriteLine($"signatur: {signatur}");
Console.WriteLine($"expected: {expectedSignatur}");
Console.WriteLine(signatur.Equals(expectedSignatur));
}
}

我个人会将您的 HmacSha256 方法更改为:

private static byte[] ComputeHmacSha256Hash(byte[] key, byte[] data)
{
using (var hmac = new HMACSHA256(key))
{
return hmac.ComputeHash(data);
}
}

因此它更通用,为了方便起见,可能会在编码为 UTF-8 后添加另一种方法来计算哈希值。这样您就可以签署任何数据,而不仅仅是字符串。

关于c# Base64编码解码错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com