title: Get PEM public and private keys or from PFX file
body: <p>I have a PEM file that was generated with <code>openssl</code> using the following command:</p>
  <pre><code>.\openssl.exe  pkcs12 -in &quot;C:\temp\mytest.pfx&quot; -nokeys -out &quot;C:\temp\mytest.publicchain.pem&quot;
  </code></pre>
  <p>This generated a PEM file with the following content:</p>
  <pre><code>-----BEGIN CERTIFICATE-----
  MIIJbTCCByGgAwIBAgITfgAAADLWmZPQJCEhKgAAAAAAMjBBBgkqhkiG9w0BAQow
  NKAPMA0GCWCGSAFlAwQCAwUAoRwwGgYJKoZIhvcNAQEIMA0GCWCGSAFlAwQCAwUA
  ... [removed rest of key for obvious reasons]
  -----END CERTIFICATE-----
  
  -----BEGIN CERTIFICATE-----
  MIIHFzCCBP+gAwIBAgITHgAAAAR0/hITgHTMqgAAAAAABDANBgkqhkiG9w0BAQ0F
  ADAcMRowGAYDVQQDExFFbmVjby1TQkRULVJvb3RDQTAeFw0xOTA4MjcyMjMwMjFa
  ... [removed rest of key for obvious reasons]
  -----END CERTIFICATE-----
  
  -----BEGIN CERTIFICATE-----
  MIIFsDCCA5igAwIBAgIQM4wn3z5DLaxG8ARk2MpMJTANBgkqhkiG9w0BAQ0FADAc
  MRowGAYDVQQDExFFbmVjby1TQkRULVJvb3RDQTAeFw0xODA5MTcwNzIyNDhaFw0y
  ... [removed rest of key for obvious reasons]
  -----END CERTIFICATE-----
  </code></pre>
  <p>I am also using the <code>AxualFramework</code> from Apache, and I need to provide it with a PEM public and private key. Their description is:</p>
  <blockquote>
  <p>Client's public key string (PEM format) used for authentication.</p>
  <p>Client's private key string (PEM format) used for authentication.</p>
  </blockquote>
  <p>Now my question is, how can I extract this from either my PEM file or from the PFX file? Which ever is easier to do?</p>
  <p>I also have BouncyCastle installed as a Nuget package, but I'm not sure what methods to use.</p>
  <p>I also tried to get it from the PFX file using things like:</p>
  <pre><code>X509Certificate2 cert = certificateCollection[0];
  var pubKey = cert.PublicKey.Key.ExportSubjectPublicKeyInfo();
  var pubKeyString = Convert.ToBase64String(pubKey); // Is this even correct?
  
  // how to get private key...?
  </code></pre>
  <p>But I really have no clue if this is the way to do it...</p>
  <p>Can any guide me in the right direction?</p>