Export (0) Print
Expand All

X509Certificate2.Import Method (Byte[], String, X509KeyStorageFlags)

Populates an X509Certificate2 object using data from a byte array, a password, and flags for determining how to import the private key.

Namespace:  System.Security.Cryptography.X509Certificates
Assembly:  System (in System.dll)

[PermissionSetAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Unrestricted = true)]
public override void Import(
	byte[] rawData,
	string password,
	X509KeyStorageFlags keyStorageFlags
)

Parameters

rawData
Type: System.Byte[]
A byte array containing data from an X.509 certificate.
password
Type: System.String
The password required to access the X.509 certificate data.
keyStorageFlags
Type: System.Security.Cryptography.X509Certificates.X509KeyStorageFlags
A bitwise combination of the enumeration values that control where and how to import the private key of the certificate.

This method can be used to populate an X509Certificate2 object using a password for the certificate represented by the byte array. The X509KeyStorageFlags value can be used to control where and how to import the private key.

This method accepts a byte array and can be used for certificate types such as Base64-encoded or DER-encoded X.509 certificates or PFX/PKCS12 certificates. Note that a PFX/PKCS12 certificate can contain more than one certificate. In that case, the first certificate associated with a private key is used or, if no private key is found, the first certificate is used.

Security noteSecurity Note

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (MSIL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

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.


using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;


class CertInfo
{
	//Reads a file.
	internal static byte[] ReadFile (string fileName)
	{
		FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
		int size = (int)f.Length;
		byte[] data = new byte[size];
		size = f.Read(data, 0, size);
		f.Close();
		return data;
	}
	//Main method begins here.
	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.
			byte[] rawData = 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.
			X509Store store = new X509Store();
			store.Open(OpenFlags.MaxAllowed);
			store.Add(x509);
			store.Close();
		}

		catch (DirectoryNotFoundException)
			{
				   Console.WriteLine("Error: The directory specified could not be found.");
			}
		catch (IOException)
			{
				Console.WriteLine("Error: A file in the directory could not be accessed.");
			}
		catch (NullReferenceException)
			{
				Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.");
			}
	}

}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft