使用显式 RSA 密钥对在 C# 中创建 CSR [英] Create a CSR in C# using an explicit RSA key-pair

查看:41
本文介绍了使用显式 RSA 密钥对在 C# 中创建 CSR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 OpenSSL 库,您可以通过以下方式创建 CSR(证书签名请求):

Using the OpenSSL libraries one can create a CSR (certificate signing request) by doing this:

openssl genrsa -out rsa.key 1024
openssl req -new -key rsa.key -out output.csr -config config.txt

其中 config.txt 包含要在证书中使用的专有名称.

where config.txt contains the distinguished name to use in the certificate.

我想在 Windows 下使用 C# 做类似的事情.但是,方法 createPKCS10 不需要您提供 RSA 密钥.

I would like to do something similar under Windows using C#. However, the method createPKCS10 does not require you to supply an RSA key.

有没有办法让 C# 生成显式的 RSA 私钥,然后使用该私钥创建 CSR?

Is there a way to get C# to generate an explicit RSA private key and then use that private key to create the CSR?

推荐答案

您可以使用 OpenSSL.NET库来完成这个任务.以下例程应该是您所需要的:

You can use the OpenSSL.NET library to accomplish this task. The following routines should be what you need:

public static void Main() 
{
    Console.Write(GenerateCsr(GenerateRsaKeyPair()));
}

/// <summary>
/// Generates a 2048 bit RSA key pair.
/// </summary>
/// <returns>The key container</returns>
public static CryptoKey GenerateRsaKeyPair()
{
    using(var rsa = new RSA())
    {
        rsa.GenerateKeys(2048, 0x10021, null, null);
        return new CryptoKey(rsa);
    }
}

/// <summary>
/// Generates a CSR file content using to the hard-coded details and the given key.
/// </summary>
/// /// <param name="key">RSA key to be used</param>
/// <returns>The CSR file content</returns>
public static string GenerateCsr(CryptoKey key)
{
    using (var subject = new X509Name
    {
        SerialNumber = "1234567890",
        Organization = "My Company"
        // Add more details here...
    })
    {
        using (var req = new X509Request(0, subject, key))
        {
            return req.PEM;
        }
    }
}

这篇关于使用显式 RSA 密钥对在 C# 中创建 CSR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆