Search code examples
c#cryptographycrt

Extract public key from certificate


Is there a way to extract the public key from a certificate using C#? I have a certificate file with .crt extension.


Solution

  • Use:

    certificate = new X509Certificate2("server.crt", "secret_password");
    byte[] publicKey = certificate.PublicKey.EncodedKeyValue.RawData;
    

    now the 'publicKey' byte array is the ASN.1-encoded representation of the public key value.