Microsoft Docs

X509Certificate2.PublicKey Property

Gets the public key of the certificate.

Property Value

Type: System.Security.Cryptography.PublicKey

A System.Security.Cryptography.PublicKey object that contains the public key of the certificate.

Remarks

The PublicKey property returns the public key information for the certificate. This information is represented by a PublicKey object, which can be used to access details about the public key, such as its encoded data and its parameters.

The public key is a critical component of asymmetric cryptography, used in operations like encryption and digital signature verification. When you obtain an X509Certificate2 object, you can easily access its associated public key through this property.

This property is particularly useful when you need to perform operations that require the public key, such as:

  • Verifying digital signatures.
  • Encrypting data that can only be decrypted by the corresponding private key.
  • Extracting specific cryptographic parameters associated with the public key.

Examples

Getting the Public Key from a Certificate

using System; using System.Security.Cryptography.X509Certificates; public class CertificatePublicKeyExample { public static void Main(string[] args) { // Assume 'certificate' is an initialized X509Certificate2 object. // For demonstration, let's create a dummy certificate. // In a real application, you would load a certificate from a store or file. X509Certificate2 certificate = null; try { // Attempt to find a suitable certificate for demonstration X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); if (store.Certificates.Count > 0) { certificate = store.Certificates[0]; // Use the first certificate found Console.WriteLine($"Using certificate: {certificate.SubjectName.Name}"); } else { Console.WriteLine("No certificates found in the CurrentUser My store. Cannot demonstrate PublicKey property."); return; } store.Close(); } catch (Exception ex) { Console.WriteLine($"An error occurred while trying to access certificates: {ex.Message}"); return; } if (certificate != null) { // Get the PublicKey object PublicKey publicKey = certificate.PublicKey; Console.WriteLine($"\nPublic Key Information:"); Console.WriteLine($" Algorithm: {publicKey.Oid.FriendlyName} ({publicKey.Oid.Value})"); Console.WriteLine($" Key Blob Length: {publicKey.KeyBlob.Length} bytes"); Console.WriteLine($" Encoded Key (first 50 chars): {publicKey.EncodedKey.ToBase64String(0, Math.Min(50, publicKey.EncodedKey.Length))}"); // You can also access the RSAParameters if it's an RSA key if (publicKey.Key is System.Security.Cryptography.RSA rsaKey) { RSAParameters rsaParams = rsaKey.ExportParameters(false); // Export public parameters only Console.WriteLine($"\nRSA Public Parameters:"); Console.WriteLine($" Modulus Length: {rsaParams.Modulus.Length} bytes"); Console.WriteLine($" Exponent Length: {rsaParams.Exponent.Length} bytes"); } } } }

Requirements

Namespace: System.Security.Cryptography.X509Certificates

Assembly: System.Security.Cryptography.X509Certificates.dll