gpt4 book ai didi

c# JWT 将 ES256 PEM 文件加载到 CngKey (jose-jwt)

转载 作者:行者123 更新时间:2023-11-30 16:48:28 27 4
gpt4 key购买 nike

作为快速概述,我正在尝试使用 https://github.com/dvsekhvalnov/jose-jwt 通过 C# 生成 ES256 算法 -JWT token 图书馆。

如说明所述:

ES256, ES384, ES256 ECDSA signatures requires CngKey (usually private) elliptic curve key of corresponding length. Normally existing CngKey loaded via CngKey.Open(..) method from Key Storage Provider. But if you want to use raw key material (x,y) and d, jose-jwt provides convenient helper EccKey.New(x,y,d).

CngKey.Open() 声明它打开一个现有 key ,但听上去我应该改用 CngKey.Import() 吗?当我尝试调用 CngKey.Import() 时,它返回以下错误:

The parameter is incorrect.

基本上我要问的是,将现有 PEM 文件转换为 Jose.JWT.Encode() 函数所需的 CngKey 对象的最简单方法是什么?任何帮助将不胜感激。谢谢!

下面是我的代码(出于安全目的,不是真正的私钥):

public string GenerateToken(int contactID, Database _db)
{
var contact = GetContact(contactID, _db);
var payload = new Dictionary<string, object>()
{
{"broker", 1},
{"contact_id", contact.id},
{"name", contact.fname + " " + contact.lname + ""},
{"iss", "www.somewhere.com"},
{"iat", (DateTime.Now - UnixEpoch).TotalSeconds},
{"nbf", (DateTime.Now - UnixEpoch).TotalSeconds},
{"exp", (DateTime.Now.AddDays(30) - UnixEpoch).TotalSeconds}
};

string privateKey =
"MHcCAQEffEIIIHHHHHHHHHHHHHHHffHHHHHHHHHHHHHHHHHHHHHHHoGgCCqGSM49" +
"AwEHhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhI+pRkAxAb13" +
"77vz2Yjjjjjjjjjjjjjjjjjjjjw==";
byte[] b = Convert.FromBase64String(privateKey);

CngKey cng = CngKey.Import(b, CngKeyBlobFormat.EccPrivateBlob);
string token = Jose.JWT.Encode(payload, cng, JwsAlgorithm.ES256);
return token;
}

最佳答案

我在使用 jose-jwt 时遇到了同样的问题,并使用我自己的 GetECDsaPrivateKey() 实现让它工作。请注意,您的项目应以 .NET 4.6.1 为目标。请按照以下步骤操作:

1.使用openssl生成一个p12 X509Certificate2

> openssl ecparam -name prime256v1 -genkey > private-key.pem
> openssl ec -in private-key.pem -pubout -out public-key.pem
> openssl req -new -key private-key.pem -x509 -nodes -days 365 -out public.cer
> winpty openssl pkcs12 -export -in public.cer -inkey private-key.pem -out publiccert.p12

2.通过读取上面生成的证书中的私钥生成一个JWT:

var claims = new Dictionary<string, object>()
{
{ "sub", "mr.x@contoso.com" },
{ "exp", 1300819380 }
};

var certificate = new X509Certificate2("publiccert.p12", "passcode");
string token = SignJWTWithCert(certificate, claims);

private static string SignJWTWithCert(X509Certificate2 cert, object claims)
{
var header = new { alg = "ES256", typ = "JWT" };
byte[] headerBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header, Formatting.None));
byte[] claimsBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(claims, Formatting.None));

using (ECDsa ecdsa = cert.GetECDsaPrivateKey())
{
if (ecdsa == null)
throw new ArgumentException("Cert must have an ECDSA private key", nameof(cert));

var payload = Base64UrlEncode(headerBytes) + "." + Base64UrlEncode(claimsBytes);
var signature = ecdsa.SignData(Encoding.UTF8.GetBytes(payload), HashAlgorithmName.SHA256);
return payload + "." + Base64UrlEncode(signature);
}
}

关于c# JWT 将 ES256 PEM 文件加载到 CngKey (jose-jwt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129214/

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