8

有没有办法创建 .pfx 文件来签署文件,我找到了一个名为 x509 Certificate Generate 的程序,但我想知道它是否可以使用 c# 在代码中生成。

4

6 回答 6

6

有一个 Microsoft 命令行工具makecert可用于生成证书。它是 Windows SDK 的一部分。在我的机器上有六个版本,例如C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64. 然后在您的代码中,您可以启动一个进程以使用适当的参数运行可执行文件。

于 2012-01-19T01:03:48.853 回答
3

您可以查看Bouncy Castle API,它可用于使用 C# 生成证书。

于 2012-01-19T01:06:23.960 回答
1

首先,除非您的组织中有自定义 PKI 层次结构,否则使用自签名证书对文档进行签名是没有意义的(在后一种情况下,您需要清楚自己在做什么,但似乎并非如此)。

PFX 是一个包含一个或多个具有关联私钥的证书的容器。所以你不会生成“PFX 文件”。您生成一个密钥对并创建一个证书,然后可以将其导出为 PFX 文件或其他格式。

如上所述,BouncyCastle 可以生成证书,我们的 SecureBlackbox 库也可以生成证书并将它们保存到/从许多不同的格式中加载。

于 2012-01-19T06:47:26.467 回答
0

You can make digital signature by using adobe reader

if you are using adobe x -then go to 3rd option from left -here you can see signing setting -open this and go to add id -just enter your details and your are done .pfx file is ready where ever you browsed it.... -it is valid for 6 years

于 2013-07-23T17:01:00.643 回答
0

如果您像@David Clarke 的答案所指的那样阅读有关makecert 的信息,您会看到满足您的要求,您只需要一个用.NET 编写的托管makecert。

幸运的是,Mono 的人很久以前就实现了这一点,

https://github.com/mono/mono/blob/master/mcs/tools/security/makecert.cs

于 2012-01-19T06:59:33.483 回答
0

您可以为此使用OpenSSL.NET库。

以下是如何执行此操作的代码示例:

    public X509Certificate2 GeneratePfxCertificate(string certificatePath, string privateKeyPath,
        string certificatePfxPath, string rootCertificatePath, string pkcs12Password)
    {
        string keyFileContent = File.ReadAllText(privateKeyPath);
        string certFileContent = File.ReadAllText(certificatePath);
        string rootCertFileContent = File.ReadAllText(rootCertificatePath);

        var certBio = new BIO(certFileContent);
        var rootCertBio = new BIO(rootCertFileContent);

        CryptoKey cryptoKey = CryptoKey.FromPrivateKey(keyFileContent, string.Empty);
        var certificate = new OpenSSL.X509.X509Certificate(certBio);
        var rootCertificate = new OpenSSL.X509.X509Certificate(rootCertBio);

        using (var certChain = new Stack<OpenSSL.X509.X509Certificate> { rootCertificate })
        using (var p12 = new PKCS12(pkcs12Password, cryptoKey, certificate, certChain))
        using (var pfxBio = BIO.MemoryBuffer())
        {
            p12.Write(pfxBio);
            var pfxFileByteArrayContent =
                pfxBio.ReadBytes((int)pfxBio.BytesPending).Array;

            File.WriteAllBytes(certificatePfxPath, pfxFileByteArrayContent);
        }

        return new X509Certificate2(certificatePfxPath, pkcs12Password);
    }
于 2019-11-22T12:38:00.863 回答