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

标签 c# windows

使用 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/

相关文章:

c++ - 如何在 C++ 中打开/等待一个程序完成然后运行另一个程序?

java - 如何提供关闭 Internet Explorer 窗口的确认?

C# 同时使用 2 个 sqldatareader?

c# - 在 Unity 中构建强大的体验系统

c# - C# 是否支持返回类型的类型推断?

java - 在 `javac.exe` 中找不到 `jdk\bin`

c# - Moq:如何获取传递给模拟服务方法的参数

c# - 企业图书馆强命名指导包

r - win-builder 找不到 gcc

python - 在扩展屏幕上使用带有 pyautogui 的鼠标 [python]