X509Certificate2.Import 方法

定義

用提供的憑證資訊,填入 X509Certificate2 物件。Populates an X509Certificate2 object with the certificate information provided.

多載

Import(Byte[])

用位元組陣列的資料,填入 X509Certificate2 物件。Populates an X509Certificate2 object with data from a byte array.

Import(String)

用憑證檔的資訊,填入 X509Certificate2 物件。Populates an X509Certificate2 object with information from a certificate file.

Import(Byte[], SecureString, X509KeyStorageFlags)

使用位元組陣列的資料、密碼和金鑰儲存旗標,填入 X509Certificate2 物件。Populates an X509Certificate2 object using data from a byte array, a password, and a key storage flag.

Import(Byte[], String, X509KeyStorageFlags)

用位元組陣列的資料、密碼以及用於判斷如何匯入私密金鑰的旗標,填入 X509Certificate2 物件。Populates an X509Certificate2 object using data from a byte array, a password, and flags for determining how to import the private key.

Import(String, SecureString, X509KeyStorageFlags)

用憑證檔的資訊、密碼和金鑰儲存旗標,填入 X509Certificate2 物件。Populates an X509Certificate2 object with information from a certificate file, a password, and a key storage flag.

Import(String, String, X509KeyStorageFlags)

用憑證檔的資訊、密碼和 X509Certificate2 值,填入 X509KeyStorageFlags 物件。Populates an X509Certificate2 object with information from a certificate file, a password, and a X509KeyStorageFlags value.

Import(Byte[])

用位元組陣列的資料,填入 X509Certificate2 物件。Populates an X509Certificate2 object with data from a byte array.

public:
 override void Import(cli::array <System::Byte> ^ rawData);
public override void Import (byte[] rawData);
override this.Import : byte[] -> unit
Public Overrides Sub Import (rawData As Byte())

參數

rawData
Byte[]

位元組陣列,包含來自 X.509 憑證的資料。A byte array containing data from an X.509 certificate.

備註

這個方法可以用來取得 x.509 憑證的原始位元組陣列,並 X509Certificate2 以其相關聯的值填入物件。This method can be used to take a raw byte array of an X.509 certificate and populate the X509Certificate2 object with its associated values.

由於這個方法只接受位元組陣列,因此只能用於不需要密碼的憑證類型,包括 Base64 編碼或 DER 編碼的 x.509 憑證或 PFX/PKCS12 憑證。Since this method accepts only a byte array, it can be used only for certificate types that do not require a password, including a Base64-encoded or DER-encoded X.509 certificate or a PFX/PKCS12 certificate. 請注意,PFX/PKCS12 憑證可以包含一個以上的憑證。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.

適用於

Import(String)

用憑證檔的資訊,填入 X509Certificate2 物件。Populates an X509Certificate2 object with information from a certificate file.

public:
 override void Import(System::String ^ fileName);
public override void Import (string fileName);
override this.Import : string -> unit
Public Overrides Sub Import (fileName As String)

參數

fileName
String

憑證的名稱。The name of a certificate.

備註

這個方法會使用憑證檔案(例如副檔名為 .cer 的檔案)來表示 x.509 憑證,並使用檔案包含的憑證來擴展 X509Certificate2 物件。This method uses a certificate file, such as a file with a .cer extension, that represents an X.509 certificate and populates the X509Certificate2 object with the certificate the file contains.

此方法可搭配數種憑證類型使用,包括 Base64 編碼或 DER 編碼的 x.509 憑證、PFX/PKCS12 憑證和簽署者憑證,例如 Authenticode。This method can be used with several certificate types, including Base64-encoded or DER-encoded X.509 certificates, PFX/PKCS12 certificates, and signer certificates such as Authenticode. 請注意,PFX/PKCS12 憑證可以包含一個以上的憑證。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.

適用於

Import(Byte[], SecureString, X509KeyStorageFlags)

重要

此 API 不符合 CLS 規範。

使用位元組陣列的資料、密碼和金鑰儲存旗標,填入 X509Certificate2 物件。Populates an X509Certificate2 object using data from a byte array, a password, and a key storage flag.

public:
 override void Import(cli::array <System::Byte> ^ rawData, System::Security::SecureString ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
public override void Import (byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
public override void Import (byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
override this.Import : byte[] * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
[<System.CLSCompliant(false)>]
override this.Import : byte[] * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (rawData As Byte(), password As SecureString, keyStorageFlags As X509KeyStorageFlags)

參數

rawData
Byte[]

位元組陣列,包含來自 X.509 憑證的資料。A byte array that contains data from an X.509 certificate.

password
SecureString

存取 X.509 憑證資料所需的密碼。The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

列舉值的位元組合,會控制匯入憑證的位置和方式。A bitwise combination of the enumeration values that control where and how to import the certificate.

屬性

備註

您可以使用此方法來進行憑證類型,例如 Base64 編碼或 DER 編碼的 x.509 憑證或 PFX/PKCS12 憑證。You can use this method for certificate types such as Base64-encoded or DER-encoded X.509 certificates, or PFX/PKCS12 certificates. 請注意,PFX/PKCS12 憑證可以包含一個以上的憑證。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.

重要

請勿在原始程式碼中將密碼硬編碼。Never hard code a password within your source code. 您可以使用 Ildasm.exe (IL 解譯器) 、十六進位編輯器,或直接在文字編輯器(例如 Notepad.exe)中開啟元件,從元件中取出硬式編碼的密碼。Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

適用於

Import(Byte[], String, X509KeyStorageFlags)

用位元組陣列的資料、密碼以及用於判斷如何匯入私密金鑰的旗標,填入 X509Certificate2 物件。Populates an X509Certificate2 object using data from a byte array, a password, and flags for determining how to import the private key.

public:
 override void Import(cli::array <System::Byte> ^ rawData, System::String ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
public override void Import (byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (byte[] rawData, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
override this.Import : byte[] * string * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (rawData As Byte(), password As String, keyStorageFlags As X509KeyStorageFlags)

參數

rawData
Byte[]

位元組陣列,包含來自 X.509 憑證的資料。A byte array containing data from an X.509 certificate.

password
String

存取 X.509 憑證資料所需的密碼。The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

列舉值的位元組合,會控制匯入憑證的位置和方式。A bitwise combination of the enumeration values that control where and how to import the certificate.

範例

下列程式碼範例會建立命令列可執行檔,它會將憑證檔案作為引數,並將各種憑證屬性列印到主控台。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.dll>

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Permissions;
using namespace System::IO;
using namespace System::Security::Cryptography::X509Certificates;

//Reads a file.
array<Byte>^ ReadFile( String^ fileName )
{
   FileStream^ f = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );
   int size = (int)f->Length;
   array<Byte>^data = gcnew array<Byte>(size);
   size = f->Read( data, 0, size );
   f->Close();
   return data;
}

[SecurityPermissionAttribute(SecurityAction::LinkDemand, Unrestricted = true)]
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();

   //Test for correct number of arguments.
   if ( args->Length < 2 )
   {
      Console::WriteLine( "Usage: CertInfo <filename>" );
      return  -1;
   }

   try
   {
      System::Security::Cryptography::X509Certificates::X509Certificate2 ^ x509 =
            gcnew System::Security::Cryptography::X509Certificates::X509Certificate2;

      //Create X509Certificate2 object from .cer file.
      array<Byte>^rawData = ReadFile( args[ 1 ] );

      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 = gcnew 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." );
   }

}

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.");
        }
    }
}
Imports System.Security.Cryptography
Imports System.Security.Permissions
Imports System.IO
Imports System.Security.Cryptography.X509Certificates

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

備註

您可以使用這個方法, X509Certificate2 利用位元組陣列所代表之憑證的密碼來填入物件。This method can be used to populate an X509Certificate2 object using a password for the certificate represented by the byte array. X509KeyStorageFlags 可以使用此值來控制匯入私密金鑰的位置和方式。The X509KeyStorageFlags value can be used to control where and how to import the private key.

這個方法會接受位元組陣列,並可用於憑證類型,例如 Base64 編碼或 DER 編碼的 x.509 憑證或 PFX/PKCS12 憑證。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. 請注意,PFX/PKCS12 憑證可以包含一個以上的憑證。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.

重要

請勿在原始程式碼中將密碼硬編碼。Never hard code a password within your source code. 您可以使用 Ildasm.exe (IL 解譯器) 、十六進位編輯器,或直接在文字編輯器(例如 Notepad.exe)中開啟元件,從元件中取出硬式編碼的密碼。Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

適用於

Import(String, SecureString, X509KeyStorageFlags)

重要

此 API 不符合 CLS 規範。

用憑證檔的資訊、密碼和金鑰儲存旗標,填入 X509Certificate2 物件。Populates an X509Certificate2 object with information from a certificate file, a password, and a key storage flag.

public:
 override void Import(System::String ^ fileName, System::Security::SecureString ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
public override void Import (string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
public override void Import (string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
override this.Import : string * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
[<System.CLSCompliant(false)>]
override this.Import : string * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (fileName As String, password As SecureString, keyStorageFlags As X509KeyStorageFlags)

參數

fileName
String

憑證檔的名稱。The name of a certificate file.

password
SecureString

存取 X.509 憑證資料所需的密碼。The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

列舉值的位元組合,會控制匯入憑證的位置和方式。A bitwise combination of the enumeration values that control where and how to import the certificate.

屬性

備註

安全性注意事項 請勿在原始程式碼中將密碼硬編碼。Security Note Never hard code a password within your source code. 您可以使用 Ildasm.exe (IL 解譯器) 、十六進位編輯器,或直接在文字編輯器(例如 Notepad.exe)中開啟元件,從元件中取出硬式編碼的密碼。Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

適用於

Import(String, String, X509KeyStorageFlags)

用憑證檔的資訊、密碼和 X509Certificate2 值,填入 X509KeyStorageFlags 物件。Populates an X509Certificate2 object with information from a certificate file, a password, and a X509KeyStorageFlags value.

public:
 override void Import(System::String ^ fileName, System::String ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
public override void Import (string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
override this.Import : string * string * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (fileName As String, password As String, keyStorageFlags As X509KeyStorageFlags)

參數

fileName
String

憑證檔的名稱。The name of a certificate file.

password
String

存取 X.509 憑證資料所需的密碼。The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

列舉值的位元組合,會控制匯入憑證的位置和方式。A bitwise combination of the enumeration values that control where and how to import the certificate.

備註

您可以使用此方法,將 X509Certificate2 憑證檔案、密碼和值的資訊填入物件中 X509KeyStorageFlagsThis method can be used populate an X509Certificate2 object with information from a certificate file, a password, and an X509KeyStorageFlags value.

重要

請勿在原始程式碼中將密碼硬編碼。Never hard code a password within your source code. 您可以使用 Ildasm.exe (IL 解譯器) 、十六進位編輯器,或直接在文字編輯器(例如 Notepad.exe)中開啟元件,從元件中取出硬式編碼的密碼。Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

適用於