【问题标题】:Converting from a Public Microsoft.IdentityModel.Tokens.JsonWebKey.JsonWebKey to RSAParameters (Public Key)从公共 Microsoft.IdentityModel.Tokens.JsonWebKey.JsonWebKey 转换为 RSAParameters(公共密钥)
【发布时间】:2022-03-25 09:31:40
【问题描述】:

基本上,我希望将公共 Microsoft.IdentityModel.Tokens.JsonWebKey.JsonWebKey 转换为 RSAParameters,然后在 RSA 实例中使用。在此之后,我正在创建一个 Azure KeyVault JsonWebKey,以便我可以将此密钥导入我的保管库。我目前尝试过这个,但还没有让它工作。有什么建议/捷径吗?

var jwk = new JsonWebKey(someStr); // IdentityModel.Tokens...

var rsaParams = new RSAParameters
{
    Modulus = WebEncoders.Base64UrlDecode(jwk.N),
    Exponent = WebEncoders.Base64UrlDecode(jwk.E)
};

var rsa = RSA.Create(rsaParams);
var key = new JsonWebKey(rsa); // Azure.Security.KeyVault.Keys
....
var kvKey = keyClient.ImportKey(keyName, key); // keyClient = KeyClient class

我从这个请求中收到的错误是:

RSA key is not valid - cannot instantiate crypto service

【问题讨论】:

    标签: c# asp.net-core rsa .net-5 jwk


    【解决方案1】:

    尝试添加其他 rsa 参数,然后导入 RSACryptoServiceProvider。然后您就可以恢复 SecurityKey。

    下面的代码是如何从存储在 JsonWebKey 中的先前 RSASecurityKey 中恢复 SecurityKey 的示例

    using RSACryptoServiceProvider provider = new RSACryptoServiceProvider(2048);
    JsonWebKey key = JsonConvert.DeserializeObject<JsonWebKey>(jsonwebkeystringcontent);
    RSAParameters rsaParameters = new()
                    {
                        Modulus = WebEncoders.Base64UrlDecode(key.N),
                        Exponent = WebEncoders.Base64UrlDecode(key.E),
                        D = WebEncoders.Base64UrlDecode(key.D),
                        DP = WebEncoders.Base64UrlDecode(key.DP),
                        DQ = WebEncoders.Base64UrlDecode(key.DQ),
                        P = WebEncoders.Base64UrlDecode(key.P),
                        Q = WebEncoders.Base64UrlDecode(key.Q),
                        InverseQ = WebEncoders.Base64UrlDecode(key.QI)
                    };
                    provider.ImportParameters(rsaParameters);
                    SecurityKey Key = new RsaSecurityKey(provider.ExportParameters(true));
    

    【讨论】:

      猜你喜欢
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多