繁体   English   中英

从 RSAParams 加载 RSA 公钥

[英]Load RSA public key from RSAParams

我需要在 C# 中生成 RSA 密钥对,然后将公钥存储在数据库中,以便以后以 JWK 格式使用。

但我无法从 RSAParams.Modulus 获取字符串。

我尝试了 UTF8、UTF32 和通用编码,但仍然没有显示。

这是来自 MSDN 网站的以下代码。

try
        {
            // Create a new RSACryptoServiceProvider object.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {

                //Export the key information to an RSAParameters object.
                //Pass false to export the public key information or pass
                //true to export public and private key information.
                RSAParameters RSAParams = RSA.ExportParameters(true);

                byte[] modulus = RSAParams.Modulus;
                var str = System.Text.Encoding.UTF8.GetString(RSAParams.Modulus);
                Console.WriteLine(str);
                Console.ReadKey();

            }
        }
        catch (CryptographicException e)
        {
            //Catch this exception in case the encryption did
            // not succeed.
            Console.WriteLine(e.Message);
        }

谢谢你。

我假设您希望您的 output 为 base64。 然后您可以使用Convert.ToBase64String来转换 RSA 密钥的ExponentModulus部分:

var exponent = Convert.ToBase64String(rsaParams.Modulus);
var modulus = Convert.ToBase64String(rsaParams.Exponent);

这与您在评论中的解决方案所做的相同(请参阅.ToXmlString 的源代码),但它确实需要绕过 XML。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM