user773456
user773456

Reputation: 651

How can I generate a .pfx file?

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#.

Upvotes: 11

Views: 44438

Answers (6)

SvjMan
SvjMan

Reputation: 689

You can use OpenSSL.NET library for this.

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

Upvotes: 2

David Clarke
David Clarke

Reputation: 13246

There is a Microsoft command-line tool makecert that 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.

Upvotes: 7

abhijay
abhijay

Reputation: 9

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

Upvotes: 0

Lex Li
Lex Li

Reputation: 63123

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.

Luckily the Mono guys have implemented this a long time ago,

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

Upvotes: 1

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).

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.

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.

Upvotes: 2

Rohan West
Rohan West

Reputation: 9298

You could check out the Bouncy Castle API, it can be used to generate certificates using C#.

Upvotes: 3

Related Questions