Click here to Skip to main content
15,672,024 members
Posted 15 Dec 2004

Tagged as



68 bookmarked

X.509 Certificates in .NET

Rate me:
Please Sign up or sign in to vote.
3.39/5 (29 votes)
16 Dec 2004CPOL8 min read
The article show detail about X509 certificate and its formats with how you can implemnt in .NET


This tutorial explains all about X.509 certificates and its current formats and shows how it can be implemented in .NET environment.


A public key certificate, usually just called a digital certificate or certs is a digitally signed document that is commonly used for authentication and secure exchange of information on open networks, such as the Internet, extranets, and intranets. A certificate securely binds a public key to the entity that holds the corresponding private key. Certificates are digitally signed by the issuing certification authority (CA) and can be issued for a user, a computer, or a service. This creates a trust relationship between two unknown entities. The CA is the Grand Pooh-bah of Validation in an organization, which everyone trusts, and in some public key environments, no certificate is considered valid unless it has been attested to by a CA. Example of a popular CA’s authority is .

Certificates can be issued for a variety of functions such as Web user authentication, Web server authentication, secure e-mail (Secure/Multipurpose Internet Mail Extensions, or S/MIME), and Internet Protocol security (IPSec), Transport Layer Security (TLS), and code signing. For example, when using the Internet for online banking, it is important to know that your Web browser is communicating directly and securely with your bank's Web server. Your Web browser must be able to achieve Web server authentication before a safe transaction can occur. Microsoft Internet Explorer uses Secure Sockets Layer (SSL) to encrypt messages and transmit them securely across the Internet, as do most other modern Web browsers and Web servers.

An X.509 certificate includes the public key and information about the person or entity to whom the certificate is issued, information about the certificate, plus optional information about the certification authority (CA) issuing the certificate.

It is a public key exchange framework and OSI standard specified by ISO/IEC 9594-8.

Formats of X.509 Certificates:

  1. DER Encoded Binary X.509 (.cer)
  2. Base64 Encoded X.509 (.cer)
  3. PKCS#7 / Cryptographic Message Syntax Standard (.p7b)
  4. PKCS#12 / Personal Information Exchange (.pfx)

DER Encoded Binary X.509

DER (Distinguished Encoding Rules) for ASN.1, as defined in ITU-T Recommendation X.509, is a more restrictive encoding standard than the alternative BER (Basic Encoding Rules) for ASN.1, as defined in ITU-T Recommendation X.209, upon which DER is based. Both BER and DER provide a platform-independent method of encoding objects (such as certificates and messages) for transmission between devices and applications. During certificate encoding, most applications use DER because a portion of the certificate (the Certification Request's Certification Request Info) must be DER-encoded to be signed. This format might be used by certification authorities that are not on Windows2000 servers, so it is supported for interoperability. DER certificate files use the .cer extension.

Base64 Encoded X.509

This is an encoding method developed for use with Secure/Multipurpose Internet Mail Extensions (S/MIME) which is a popular, standard method for transferring binary attachments over the Internet. Base64 encodes files into ASCII text format, making corruption less likely as the files are sent through Internet gateways, while S/MIME provides some cryptographic security services for electronic messaging applications, including non-repudiation of origin using digital signatures, privacy and data security using encryption, authentication, and message integrity. The MIME (Multipurpose Internet Mail Extensions) specification (RFC1341 and successors) defines a mechanism for encoding arbitrary binary information for transmission by electronic mail. Because all MIME-compliant clients can decode Base64 files, this format might be used by certification authorities that are not on Windows2000 servers, so it is supported for interoperability. Base64 certificate files use the .cer extension.

Cryptographic Message Syntax Standard (PKCS#7)

The PKCS#7 format enables the transfer of a certificate and all the certificates in its certification path from one computer to another or from a computer to removable media. PKCS#7 files typically use the .p7b extension and are compatible with the ITU-TX.509 standard. PKCS#7 allows for attributes such as countersignatures to be associated with signatures. Attributes such as signing time can be authenticated along with message content. For information on PKCS #7 see

Personal Information Exchange (PKCS#12)

The Personal Information Exchange format (.pfx, also called PKCS#12) enables the transfer of certificates and their corresponding private keys from one computer to another or from a computer to removable media. PKCS#12 (Public Key Cryptography Standard#12) is an industry format that is suitable for transport or backup and restoration of a certificate and its associated private key. This can be between products from the same vendor or different vendors. To use the PKCS#12 format, the cryptographic service provider (CSP) must recognize the certificate and keys as exportable. If a certificate was issued from a Windows2000 certification authority, the private key for that certificate is only exportable if one of the following is true: The certificate is for EFS (encrypting file system) or EFS recovery. The certificate was requested through the Advanced Certificate Request certification authority Web page with the Mark keys as exportable check box selected. Because exporting a private key might expose it to unintended parties, the PKCS#12 format is the only format supported in WindowsXP for exporting a certificate and its associated private key. For information on PKCS #12 see

Let us make a new X.509 certificate and implement in .NET environment. However remember .NET 1.0 / 1.1 supports only DER Encoded Binary X.509 format only. (However at the bottom of article I will told you a way to use latest certificate formats such as PKSC #12 (.pfx) in .NET).

The most widely accepted format for certificates is defined by the ITU-T X.509 version 3 international standards. The certificates are encoded using OSI ASN.1 DER. Some primary fields in X.509 certificate are indicated below:




Which version of X.509

Serial number

This number plus the CA’s name uniquely identifies the certificate

Signature algorithm

The algorithm used to sign certificate


X.500 name of CA

Validity Period

The starting and ending period

Subject name

The entity whose key being certified

Public Key

The subject’s pubic key and ID of algorithm using it.

Microsoft provides many security tools in .NET SDK. Most of them are consoled based utilities applications. That is:

  • Permission and assembly management tools
  • Certificate management tools

We are concerned with only certificate management tools. They are


Meaning (See MSDN for more detail options of each application)


Generate a X.509 certificate for testing purpose only


Assembles certificates into CTL (certificate trust list) and can also be used for revoking lists (CRLs).


Verifies the validity of a file signed with an X.509 certificate


Creates, for test purposes only, a Software Publisher's Certificate (SPC) from one or more X.509 certificates.

Let us create a DER based X.509 certificate (.cer) with following command on the Visual Studio 03 command prompt.

makecert -sk Adnan -n "CN=Adnan Company" Adnan.cer

  • -sk is Subject name
  • –n is Specifies the subject's key container location, which contains the private key. If a key container does not exist, it will be created.

This will create a X.509 certificate (Adnan.cer) in personal folder directory of user currently logged. Now we can retrieve its properties using System.Security.Cryptography.X509Certificates class. The Certificate is also included in source code.

Now we will test this certificate and creates a Software Publisher's Certificate (SPC) file (PCKS # 7) from following command on VS command prompt.

cert2spc Adnan.cer Adnanspc.spc

This will create (Adnanspc.spc) file in personal folder directory of user currently logged. However this is for test purposes only. You can obtain a valid SPC from a Certification Authority such as VeriSign or Thawte. The spc file is included in source code.

Use Chktrust to checks the validity of a file signed with an Authenticode certificate.

chktrust signedfile

The signed file could be any valid application (exe). If application does not have a valid

signature, the tool displays the Security Warning dialog box. The dialog gives you the option

to install and run the PE file even though an Authenticode signature could not be found.

Similarly use Certmgr to manages certificates, certificate trust lists (CTLs), and certificate

revocation lists (CRLs). The following command displays a default system store called my

with verbose output (/v).

certmgr /v /s my

Code to access X.509 properties in .NET:

Start a new windows application and give it a meaningful name and you have a form name FORM1 in project.

Next add a Button on this form and write code in that button event handler as shown below:

Imports System.Security.Cryptography.X509Certificates
Imports System.IO

Private Sub Button1_Click (ByVal sender As System.Object,
 ByVal e As System.EventArgs) Handles Button1.Click
 ‘get certificate in Bin directory

 Dim Cert As X509Certificate = X509Certificate.CreateFromCertFile(
  Directory.GetCurrentDirectory & "\Adnan.cer")
 ‘Now retrieve its properties in output window using ToString Mehtod. Since this
 'class it also have many other
 ‘methods that do the same job 
 ‘Get the most important values in string format.

 Dim resultsTrue As String = Cert.ToString(True)
 ‘Display the value in output window 
 ‘Now display Serial number in bytes with GetSerialNumber() method
 Dim SerialNumber() As Byte = Cert.GetSerialNumber()

 Dim Sr As Byte
 Debug.Write("Serial Number in Bytes: ") 
 For Each Sr In SerialNumber
End Sub

PKCS #12 Certificates in .NET.

  1. We can use PKCS #12 (.pfx) certificates in .NET using latest CAPICOM wrappers available from Microsoft site
  1. Download file pvkimprt.exe directly from needed to generate PFX file (PKCS #12).

How to create PKCS #12 Certificate in .NET

The following commands in Visual studio 03 command prompt can be used to create a PFX file (CodeProject.pfx)with a self-signed certificate and the associated private key (.pvk):

  1. makecert -r -n "CN=CodeProject" -b <date year="2004" day="20" month="12">20/12/2004 -e <date year="2099" day="1" month="1">01/01/2099 -eku -sv CodeProject.pvk CodeProject.cer
  2. cert2spc CodeProject.cer CodeProject.spc
  3. pvkimprt -pfx CodeProject.spc CodeProject.pvk

(Export CodeProject.pfx)

How to use PKCS #12 Certificate in .NET

You can use PKCS #12 (.pfx) certificates in .NET using latest CAPICOM wrappers available from Microsoft site Search MSDN 2004 in Platform SDK -> Security for article “PKCS #12 File Types: Portable Protected Keys in .NET” or see online for detail. The article shows how to use CAPICOM to access PKSC #12 files in C# code however it can be easily converted into Vb .NET code.

We have seen what amazing power .NET have for management of certificates.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Pakistan Pakistan
I am software engineer and working from last four years in the information technology field. I love to do intresting tasks. I love to see diffrent places and listening muzik.

Comments and Discussions

GeneralFunny typo :) Pin
Cristian M.28-Oct-13 2:36
Cristian M.28-Oct-13 2:36 
GeneralManipulate PCKS#7 add signed data to signature detached Pin
thecobaia20-Apr-11 10:36
thecobaia20-Apr-11 10:36 
GeneralMy vote of 2 Pin
kurtmoeller9-Jun-10 12:21
kurtmoeller9-Jun-10 12:21 
GeneralMy vote of 1 Pin
Krnel3223-Feb-10 1:36
Krnel3223-Feb-10 1:36 
GeneralI&#180;m trying to develop the same solution in ASP.NET Pin
AngeloBestetti6-Feb-10 4:09
AngeloBestetti6-Feb-10 4:09 
GeneralRe: I&#180;m trying to develop the same solution in ASP.NET Pin
Adnan Samuel15-Feb-10 3:59
Adnan Samuel15-Feb-10 3:59 
GeneralMy vote of 1 Pin
Valeri Makarov16-Jan-10 20:19
Valeri Makarov16-Jan-10 20:19 
GeneralCreate X509 certificate Pin
Matteo Slaviero26-Dec-09 8:39
Matteo Slaviero26-Dec-09 8:39 
GeneralMy vote of 1 Pin
cocowalla21-Nov-09 4:33
cocowalla21-Nov-09 4:33 
GeneralMy vote of 1 Pin
YrthWyndAndFyre20-Nov-09 8:05
YrthWyndAndFyre20-Nov-09 8:05 
GeneralMy vote of 1 Pin
paragme2-Sep-09 2:43
paragme2-Sep-09 2:43 
GeneralAbout the certification validation period Pin
Roshanakak27-Apr-09 19:42
Roshanakak27-Apr-09 19:42 
QuestionHow to get .crl file context Pin
ke_er10-Nov-08 16:14
ke_er10-Nov-08 16:14 
GeneralRegarding Cryptography Pin
girishrakhe11-Jan-08 1:56
girishrakhe11-Jan-08 1:56 
Generalplagiarism Pin
dan.houck10-Dec-05 6:53
dan.houck10-Dec-05 6:53 
GeneralRe: plagiarism Pin
Jeffrey Walton20-Sep-07 9:36
Jeffrey Walton20-Sep-07 9:36 
GeneralRe: plagiarism Pin
Code Deamon17-Apr-08 2:52
Code Deamon17-Apr-08 2:52 
GeneralRe: plagiarism Pin
nahid4778-Jul-10 16:25
nahid4778-Jul-10 16:25 
GeneralRe: plagiarism PinPopular
Jon Rista10-Aug-10 8:19
Jon Rista10-Aug-10 8:19 
GeneralRe: plagiarism Pin
Member 102925765-Feb-14 22:10
Member 102925765-Feb-14 22:10 
GeneralRe: plagiarism Pin
stixoffire4-Jun-15 4:52
stixoffire4-Jun-15 4:52 
GeneralRe: plagiarism Pin
Shabana Parveen3-Oct-17 22:42
professionalShabana Parveen3-Oct-17 22:42 
GeneralDer-encoding Pin
Sara97817-Jun-05 4:18
Sara97817-Jun-05 4:18 
Generalnot work yet Pin
coloboxp23-Mar-05 5:13
coloboxp23-Mar-05 5:13 
Generallink to demo not work Pin
akorolev1018-Dec-04 21:32
akorolev1018-Dec-04 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.