gpt4 book ai didi

c# - 我们如何创建 AsymmetricSecurityKey?

转载 作者:行者123 更新时间:2023-11-30 12:38:53 25 4
gpt4 key购买 nike

我们如何在 C# 中创建 AsymmetricSecurityKey。实际上,我们正在使用 AsymetricSecurityKey 创建签名凭证,这是我们的代码:

// Define const Key this should be private secret key  stored in some safe place
string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";

// Create Security key using private key above:
// not that latest version of JWT using Microsoft namespace instead of System
var securityKey = new AsymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

// Also note that securityKey length should be >256b
// so you have to make sure that your private key has a proper length
//
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
(securityKey, SecurityAlgorithms.HmacSha256Signature);

最佳答案

您可以使用以下方式生成公钥/私钥:

public void GenerateRsaCryptoServiceProviderKey()
{
var rsaProvider = new RSACryptoServiceProvider(512);
SecurityKey key = new RsaSecurityKey(rsaProvider);
}

你应该使用下面的RsaSha256:

var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
(key, SecurityAlgorithms.RsaSha256);

关于c# - 我们如何创建 AsymmetricSecurityKey?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860690/

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