title: .NET Core 3 unable to load ECC private key
body: <p>Thanks to <a href="https://stackoverflow.com/questions/56144550/loading-an-ecc-private-key-in-net">Loading an ECC private key in .NET</a>, I'm able to load ECC private keys into .NET Core 3 and performing signature tasks with them.</p>
  
  <p>I have, however run into <em>one</em> key that cannot be loaded by <code>ECDSA.ImportPrivateKey</code>. What's weird is that looking at it with <code>openssl</code> changes the key bytes to something that .NET Core 3 <strong>can</strong> understand.</p>
  
  <p>Code to import the failing private key (this is the actual key that fails):</p>
  
  <pre class="lang-cs prettyprint-override"><code>ecdsa = ECDsa.Create();
  var pem = "MHYCAQEEH5t2Xlmsw5uqw3W9+/3nosFi6i3V901uW6ZzUpvVM0qgCgYIKoZIzj0DAQehRANCAASck2UuMxfyDYBdJC0mHNeToqMBhJuMZYSgkUNbK/xzD7e3cwr5okPx0pZdSMfDmyi1dBujtIIxFK9va1bdVAR9";
  var derArray = Convert.FromBase64String(pem);
  ecdsa.ImportECPrivateKey(derArray, out _);
  </code></pre>
  
  <p>The <code>ImportECPrivateKey</code> call fails with <code>System.Security.Cryptography.CryptographicException : ASN1 corrupted data</code> inside <code>System.Security.Cryptography.EccKeyFormatHelper.FromECPrivateKey(ReadOnlyMemory`1 keyData, AlgorithmIdentifierAsn&amp; algId, ECParameters&amp; ret)</code></p>
  
  <p>The original PEM file looks like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ cat private_key_cert_265.pem
  -----BEGIN EC PRIVATE KEY-----
  MHYCAQEEH5t2Xlmsw5uqw3W9+/3nosFi6i3V901uW6ZzUpvVM0qgCgYIKoZIzj0D
  AQehRANCAASck2UuMxfyDYBdJC0mHNeToqMBhJuMZYSgkUNbK/xzD7e3cwr5okPx
  0pZdSMfDmyi1dBujtIIxFK9va1bdVAR9
  -----END EC PRIVATE KEY-----
  </code></pre>
  
  <p>openssl converts the private key to something else:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ openssl ec -in private_key_cert_265.pem
  read EC key
  writing EC key
  -----BEGIN EC PRIVATE KEY-----
  MHcCAQEEIACbdl5ZrMObqsN1vfv956LBYuot1fdNblumc1Kb1TNKoAoGCCqGSM49
  AwEHoUQDQgAEnJNlLjMX8g2AXSQtJhzXk6KjAYSbjGWEoJFDWyv8cw+3t3MK+aJD
  8dKWXUjHw5sotXQbo7SCMRSvb2tW3VQEfQ==
  -----END EC PRIVATE KEY-----
  </code></pre>
  
  <p>Using this form of the PEM file, .NET Core 3 <strong>can</strong> import the private key.</p>
  
  <p>My question is: What is going on; Why is openssl changing the private key to another format (how can I tell which format is which?), and why can .NET Core 3 understand one format and not the other?</p>