title: JAVA 11 - elliptic curve private key - Caused by: java.security.InvalidKeyException: IOException : version mismatch: (supported: 00, parsed: 01
body: <p>Small question regarding getting an elliptic curve private key with Java please.</p>
  <p>I run this command in my terminal:</p>
  <p><code>openssl ecparam -name secp256k1 -genkey -noout -out ec-secp256k1-dummy-priv-key.pem</code></p>
  <p>This commands is working fine, generating the file fine, I can even <code>cat</code> the file, which I can see:</p>
  <pre><code>-----BEGIN EC PRIVATE KEY-----
  MHQCAQEEIDHE7OA9hgIYW427NieIsXz/qAipMVhqVUIwVcEIWwuAoAcGBSuBBAAK
  oUQDQgAEcMu2KlVzJLKQ9XfoWvF0jZ+JwbPeUekHqTYVTFK9ISoKLgBN9abxIxyc
  JumqIshcc74GUVtm/sJJoiPJNdEPEQ==
  -----END EC PRIVATE KEY-----
  </code></pre>
  <p>(please note here it is written BEGIN <strong>EC</strong> PRIVATE KEY and not BEGIN PRIVATE KEY)</p>
  <p>Now, I get ride of the <code>\n</code> and spaces, etc, basically to have the &quot;inside&quot; of the key, configured as a one line string.</p>
  <pre><code>String s = &quot;MHQCAQEEIDHE7OA9hgIYW427NieIsXz/qAipMVhqVUIwVcEIWwuAoAcGBSuBBAAKoUQDQgAEcMu2KlVzJLKQ9XfoWvF0jZ+JwbPeUekHqTYVTFK9ISoKLgBN9abxIxycJumqIshcc74GUVtm/sJJoiPJNdEPEQ==&quot;;
  </code></pre>
  <p>(please let me know if this step is wrong)</p>
  <p>What I tried: I then use this piece of code to get the private key</p>
  <pre><code>String s = &quot;MHQCAQEEIDHE7OA9hgIYW427NieIsXz/qAipMVhqVUIwVcEIWwuAoAcGBSuBBAAKoUQDQgAEcMu2KlVzJLKQ9XfoWvF0jZ+JwbPeUekHqTYVTFK9ISoKLgBN9abxIxycJumqIshcc74GUVtm/sJJoiPJNdEPEQ==&quot;;
  
          byte[] keyData = Base64.getDecoder().decode(s);
          EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(keyData);
          KeyFactory kf = KeyFactory.getInstance(&quot;EC&quot;);
          PrivateKey privKey = kf.generatePrivate(privKeySpec);
  
  </code></pre>
  <p>Unfortunately, it is yielding this error</p>
  <pre><code>SEVERE: null
  java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : version mismatch: (supported:     00, parsed:     01
  
  Caused by: java.security.InvalidKeyException: IOException : version mismatch: (supported:     00, parsed:     01
      at java.base/sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:350)
      at java.base/sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:355)
  </code></pre>
  <p>I am having a bit of a hard time understanding the issue, and how to fix this.</p>
  <p>If possible, I would like to avoid falling back to bouncy castle, avoid changing the way the key is being generated, avoid having to convert the key to yet again another format.</p>
  <p>May I ask what did I do wrong, and what is the correct way to fix this issue please?</p>
  <p>Thank you</p>