C# 如何生成 .pfx 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8919741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I generate a .pfx file?
提问by user773456
Is there any way to create .pfx files in order to sign documents, I've found a program called x509 Certificate Generate,but I want to know if it can be generated in code using c#.
有什么方法可以创建 .pfx 文件来签署文档,我找到了一个名为 x509 Certificate Generate 的程序,但我想知道它是否可以使用 c# 在代码中生成。
回答by David Clarke
There is a Microsoft command-line tool makecertthat can be used to generate certificates. It's part of the Windows SDK. On my machine there are half a dozen versions e.g. in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64. Then in your code you can start a process to run the executable with the appropriate parameters.
有一个 Microsoft 命令行工具makecert可用于生成证书。它是 Windows SDK 的一部分。在我的机器上有六个版本,例如C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64. 然后在您的代码中,您可以启动一个进程以使用适当的参数运行可执行文件。
回答by Rohan West
You could check out the Bouncy Castle API, it can be used to generate certificates using C#.
您可以查看Bouncy Castle API,它可用于使用 C# 生成证书。
回答by Eugene Mayevski 'Callback
First of all, signing documents with self-signed certificates makes no sense unless you have custom PKI hierarchy in your organization (and in the latter case you need to know well what you are doing, which seems to be not the case).
首先,除非您的组织中有自定义的 PKI 层次结构,否则使用自签名证书对文档进行签名是没有意义的(在后一种情况下,您需要清楚地知道自己在做什么,但似乎并非如此)。
PFX is a container for one or more certificates with associated private keys. So you don't generate "PFX file". You generate a keypair and create a certificate, which can then be exported to PFX file or to other format.
PFX 是一个容器,用于存放一个或多个带有关联私钥的证书。所以你不会生成“PFX文件”。您生成一个密钥对并创建一个证书,然后可以将其导出为 PFX 文件或其他格式。
As mentioned above, BouncyCastle can generate certificates, and our SecureBlackbox library also can generate certificates and save and load them to/from many different formats.
如上所述,BouncyCastle 可以生成证书,我们的 SecureBlackbox 库也可以生成证书并将它们保存到/从许多不同格式加载。
回答by Lex Li
If you read about makecert like @David Clarke's answer refers to, you will see to fulfill your requirement, you just need a managed makecert written in .NET.
如果您像@David Clarke 的回答一样阅读 makecert,您将看到满足您的要求,您只需要一个用 .NET 编写的托管 makecert。
Luckily the Mono guys have implemented this a long time ago,
幸运的是 Mono 的人很久以前就已经实现了这一点,
https://github.com/mono/mono/blob/master/mcs/tools/security/makecert.cs
https://github.com/mono/mono/blob/master/mcs/tools/security/makecert.cs
回答by abhijay
You can make digital signature by using adobe reader
您可以使用 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
如果您使用的是 adobe x -然后转到左侧的第三个选项 - 在这里您可以看到签名设置 - 打开它并转到添加 id - 只需输入您的详细信息即可完成 .pfx 文件在您浏览它的任何地方都已准备就绪。 .. - 有效期为 6 年
回答by SvjMan
You can use OpenSSL.NETlibrary for this.
您可以为此使用OpenSSL.NET库。
Here is the code example how to do it:
这是如何做到这一点的代码示例:
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);
}

