gpt4 book ai didi

c# - 使用显式 RSA key 对在 C# 中创建 CSR

转载 作者:可可西里 更新时间:2023-11-01 10:29:54 25 4
gpt4 key购买 nike

使用 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 key 。

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

最佳答案

您可以使用 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;
}
}
}

关于c# - 使用显式 RSA key 对在 C# 中创建 CSR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2953088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com