首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何创建AsymmetricSecurityKey?

如何创建AsymmetricSecurityKey?
EN

Stack Overflow用户
提问于 2018-02-19 14:34:20
回答 3查看 6.2K关注 0票数 5

如何在c#中创建AsymmetricSecurityKey。实际上,我们正在使用AsymetricSecurityKey创建签名凭据,下面是我们的代码:

代码语言:javascript
运行
复制
// 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);
EN

回答 3

Stack Overflow用户

发布于 2018-02-19 14:52:02

您可以使用以下命令生成公钥/私钥:

代码语言:javascript
运行
复制
public void GenerateRsaCryptoServiceProviderKey()
{
        var rsaProvider = new RSACryptoServiceProvider(512);
        SecurityKey key = new RsaSecurityKey(rsaProvider);      
}

您应该使用下面的RsaSha256

代码语言:javascript
运行
复制
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
                  (key, SecurityAlgorithms.RsaSha256);
票数 4
EN

Stack Overflow用户

发布于 2019-10-24 15:44:54

您是否专门在寻找AsymmetricSecurityKey

我注意到您引用的是HM256算法。这让我相信你在找一个SymmetricSecurityKey。此外,您的方法似乎非常特定于使用HMAC alg。

要生成SymmetricSecurityKey,可以尝试执行类似以下代码的操作:

代码语言:javascript
运行
复制
// 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 SymmetricSecurityKey(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);

如果你想要一个使用RS256 alg的解决方案(它将使用pfx格式的证书),你可以发表评论,我会尽我最大的努力给你一个例子。

票数 -1
EN

Stack Overflow用户

发布于 2020-11-05 01:07:42

这将从F#中的RSA公钥创建安全密钥。

代码语言:javascript
运行
复制
    let pem = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnzyis1ZjfNB0bBgKFMSvvkTtwlvBsaJq7S5wA+kzeVOVpVWwkWdVha4s38XM/pa/yr47av7+z3VTmvDRyAHcaT92whREFpLv9cj5lTeJSibyr/Mrm/YtjCZVWgaOYIhwrXwKLqPr/11inWsAkfIytvHWTxZYEcXLgAXFuUuaS3uF9gEiNQwzGTU1v0FqkqTBr4B8nW3HCN47XUu0t8Y0e+lf4s4OxQawWD79J9/5d3Ry0vbV3Am1FtGJiJvOwRsIfVChDpYStTcHTCMqtvWbV6L11BWkpzGXSW4Hv43qa+GSYOD2QU68Mb59oSk2OB+BtOLpJofmbGEGgvmwyCI9MwIDAQAB"
        
    let getPublicKey (pem: string) =
            let publicKey = ReadOnlySpan<byte>(Convert.FromBase64String(pem))
            let rsa = RSA.Create()
            let mutable read = 0
            rsa.ImportSubjectPublicKeyInfo(publicKey, &read)
            new RsaSecurityKey(rsa)

    getPublicKey pem
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48860690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档