【问题标题】:Create a CSR in C# using an explicit RSA key-pair使用显式 RSA 密钥对在 C# 中创建 CSR
【发布时间】:2010-06-01 20:29:09
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • CSR 通常会包含您的密钥对中的“公钥”。我猜你是这个意思。

标签: c# windows


【解决方案1】:

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

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;
        }
    }
}

【讨论】:

    【解决方案2】:

    这是用于在 C# 中生成 .CSR 文件的代码。我正在使用 Bouncy 城堡库。

            var subjectName = "CN=www.copanyName.com,O=Company Name,OU=Department,T=Area,ST=State,C=Country";
    
            // Create new Object for Issuer and Subject
            var issuer = new X509Name(subjectName);
            var subject = new X509Name(subjectName);
    
            // Generate the key Value Pair, which in our case is a public Key
            var randomGenerator = new CryptoApiRandomGenerator();
            var random = new SecureRandom(randomGenerator);
            AsymmetricCipherKeyPair subjectKeyPair = default(AsymmetricCipherKeyPair);
            const int strength = 2048;
            var keyGenerationParameters = new KeyGenerationParameters(random, strength);
    
            var keyPairGenerator = new RsaKeyPairGenerator();
            keyPairGenerator.Init(keyGenerationParameters);
            subjectKeyPair = keyPairGenerator.GenerateKeyPair();
            AsymmetricCipherKeyPair issuerKeyPair = subjectKeyPair;
    
            //PKCS #10 Certificate Signing Request
            Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA1WITHRSA", subject, issuerKeyPair.Public, null, issuerKeyPair.Private);
    
            //Convert BouncyCastle CSR to .PEM file.
            StringBuilder CSRPem = new StringBuilder();
            PemWriter CSRPemWriter = new PemWriter(new StringWriter(CSRPem));
            CSRPemWriter.WriteObject(csr);
            CSRPemWriter.Writer.Flush();
    
            //get CSR text
            var CSRtext = CSRPem.ToString();
    
            // Write content into a Txt file
            using (StreamWriter f = new StreamWriter(@"C:\Cert_TEST\DemoCSR.txt"))
            {
                f.Write(CSRtext);
            }
    

    【讨论】:

      【解决方案3】:

      在我看来,您可以在这里找到问题的答案:http://msdn.microsoft.com/en-us/library/ms867026.aspx

      查看Reading a certificate signing request with c# 以了解相关问题。

      【讨论】:

        猜你喜欢
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 2019-05-24
        • 2020-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多