X509Certificate2 Class
Assembly: System (in system.dll)
The X.509 structure originated in the International Organization for Standardization (ISO) working groups. This structure can be used to represent various types of information including identity, entitlement, and holder attributes (permissions, age, sex, location, affiliation, and so forth). Although the ISO specifications are most informative on the structure itself, the X509Certificate2 class is designed to model the usage scenarios defined in specifications issued by the Internet Engineering Task Force (IETF) Public Key Infrastructure, X.509 (PKIX) working group. The most informative of these specifications is RFC 3280, "Certificate and Certificate Revocation List (CRL) Profile."
| Topic | Location |
|---|---|
| How to: Encrypt XML Elements with X.509 Certificates | .NET Framework: Security |
| How to: Decrypt XML Elements with X.509 Certificates | .NET Framework: Security |
| How to: Encrypt XML Elements with X.509 Certificates | .NET Framework: Security |
| How to: Decrypt XML Elements with X.509 Certificates | .NET Framework: Security |
The following code example creates a command-line executable that takes a certificate file as an argument and prints various certificate properties to the console.
Imports System Imports System.Security.Cryptography Imports System.Security.Permissions Imports System.IO Imports System.Security.Cryptography.X509Certificates Imports Microsoft.VisualBasic Class CertInfo 'Reads a file. Friend Shared Function ReadFile(ByVal fileName As String) As Byte() Dim f As New FileStream(fileName, FileMode.Open, FileAccess.Read) Dim size As Integer = Fix(f.Length) Dim data(size) As Byte size = f.Read(data, 0, size) f.Close() Return data End Function <SecurityPermission(SecurityAction.LinkDemand, Unrestricted:=True)> _ Shared Sub Main(ByVal args() As String) 'Test for correct number of arguments. If args.Length < 1 Then Console.WriteLine("Usage: CertInfo <filename>") Return End If Try Dim x509 As New X509Certificate2() 'Create X509Certificate2 object from .cer file. Dim rawData As Byte() = ReadFile(args(0)) x509.Import(rawData) 'Print to console information contained in the certificate. Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine, x509.Subject) Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine, x509.Issuer) Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine, x509.Version) Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore) Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter) Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint) Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber) Console.WriteLine("{0}Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName) Console.WriteLine("{0}Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(True)) Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length) Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(True)) Console.WriteLine("{0}Certificate to XML String: {1}{0}", Environment.NewLine, x509.PublicKey.Key.ToXmlString(False)) 'Add the certificate to a X509Store. Dim store As New X509Store() store.Open(OpenFlags.MaxAllowed) store.Add(x509) store.Close() Catch dnfExcept As DirectoryNotFoundException Console.WriteLine("Error: The directory specified could not be found.") Catch ioExpcept As IOException Console.WriteLine("Error: A file in the directory could not be accessed.") Catch nrExcept As NullReferenceException Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.") End Try End Sub End Class
import System.*;
import System.Security.Cryptography.*;
import System.Security.Permissions.*;
import System.IO.*;
import System.Security.Cryptography.X509Certificates.*;
//Permission demand to access files in c:\Test directory with.cer suffix.
/** @assembly FileIOPermissionAttribute(SecurityAction.RequestMinimum,
All = "C:\\Test#@@#.cer")
*/
class CertInfo
{
//Reads a file.
static ubyte[] ReadFile(String fileName)
{
FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
int size = (int)(f.get_Length());
ubyte data[] = new ubyte[size];
size = f.Read(data, 0, size);
f.Close();
return data;
} //ReadFile
/** @attribute SecurityPermission(SecurityAction.LinkDemand,
Unrestricted = true) */
static void main(String[] args)
{
//Test for correct number of arguments.
if (args.length < 1) {
Console.WriteLine("Usage: CertInfo <filename>");
return;
}
try {
X509Certificate2 x509 = new X509Certificate2();
//Create X509Certificate2 object from.cer file.
ubyte rawData[] = ReadFile(args[0]);
x509.Import(rawData);
//Print to console information contained in the certificate.
Console.WriteLine("{0}Subject: {1}{0}", Environment.get_NewLine(),
x509.get_Subject());
Console.WriteLine("{0}Issuer: {1}{0}", Environment.get_NewLine(),
x509.get_Issuer());
Console.WriteLine("{0}Version: {1}{0}", Environment.get_NewLine(),
(Int32)x509.get_Version());
Console.WriteLine("{0}Valid Date: {1}{0}",
Environment.get_NewLine(), x509.get_NotBefore());
Console.WriteLine("{0}Expiry Date: {1}{0}",
Environment.get_NewLine(), x509.get_NotAfter());
Console.WriteLine("{0}Thumbprint: {1}{0}",
Environment.get_NewLine(), x509.get_Thumbprint());
Console.WriteLine("{0}Serial Number: {1}{0}",
Environment.get_NewLine(), x509.get_SerialNumber());
Console.WriteLine("{0}Friendly Name: {1}{0}",
Environment.get_NewLine(),
x509.get_PublicKey().get_Oid().get_FriendlyName());
Console.WriteLine("{0}Public Key Format: {1}{0}",
Environment.get_NewLine(),
x509.get_PublicKey().get_EncodedKeyValue().Format(true));
Console.WriteLine("{0}Raw Data Length: {1}{0}",
Environment.get_NewLine(), (Int32)x509.get_RawData().length);
Console.WriteLine("{0}Certificate to string: {1}{0}",
Environment.get_NewLine(), x509.ToString(true));
Console.WriteLine("{0}Certificate to XML String: {1}{0}",
Environment.get_NewLine(),
x509.get_PublicKey().get_Key().ToXmlString(false));
//Add the certificate to a X509Store.
X509Store store = new X509Store();
store.Open(OpenFlags.MaxAllowed);
store.Add(x509);
store.Close();
}
catch (DirectoryNotFoundException exp) {
Console.WriteLine("Error: The directory specified "
+ "could not be found.");
}
catch (IOException exp) {
Console.WriteLine("Error: A file in the directory "
+ "could not be accessed.");
}
catch (NullReferenceException exp) {
Console.WriteLine("File must be a.cer file. "
+ "Program does not have access to that type of file.");
}
} //main
} //CertInfo
System.Security.Cryptography.X509Certificates.X509Certificate
System.Security.Cryptography.X509Certificates.X509Certificate2
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.