title: Load a RSA private key in Java (algid parse error, not a sequence)
body: <p>I'm trying to load a private RSA key generated with ssl into java, my code is:</p>
  
  <p><strong>Generate the key</strong>:</p>
  
  <pre><code>openssl genrsa -out mykey.pem 1024
  </code></pre>
  
  <p><strong>Result:</strong></p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  MIICXAIBAAKBgQCUibP4fY2PA/sGMKMbU6usuIGcOAqgQjD6c2ylVo05Oz7pgjnE
  +O0l2MFRUYUGT5KKk/W+0cAXkxaQHE3n8A8X1mHT8eMDmWnzz0PeYjDE8LQmAw8R
  Y2FnVKFAB36BIjdb5FsZmCk5QYKU5+nWLMqH/j/IR5AyX5wR2SMoslUg2QIDAQAB
  AoGAeJ1s7638IhLIZtldyRXjRKi6ToFPV50IKodJxOSIXt3WE0V05ZaA84eUSxUY
  IOzCgRbuqVmnUz1USAdD18AecC8qc7tXTRALLy7q8fxklPwmGPUOvTFmI7gRMUnv
  cWrq1gySk3SKpj0YmWnuY9Xmd2+xoWLzUeFD1CROY5OTjIECQQDDlp1+Al7+duR0
  XyMlkWLIk0nIbkQ5zlTAEipzmaeTSOJi6YG3EMMz3AGuZb7tw6HFxWqeg1hyKJ+T
  cTM3WTdJAkEAwmrCDKE29n3wFOBKsZZFQbDgVOUXCBs2ubEI+ALe1DJU5BlfnrhJ
  OINRCNgnwSFNbwxDTkDpR6J6Av2ElAvNEQJAV0dVvk5Wj50Ecz2lFHWdLD41taAn
  B9igDxnMIcvWcK4cf+ENhmCPiwvJIEa8/aLIBNYErvmTtVWVaBkirrc8KQJABr+z
  +sJB6S6X/fGHRkDkKJKeRvQo54QiUzHdENbwq0cQAVcMJbNZ/1c3oen2/1JLoNY5
  I+dG8dCnEaGBT65VMQJBAIDqH1Kqs5tb51cpt6h9ot31SUVud5pSML/babwp3pRs
  1s6poreym4PkAyRug0Dgcj1zVLt25TlOHvrL9r3Swq8=
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p><strong>Load:</strong></p>
  
  <pre><code>String privKeyPEM=readFile("mykey.pem");
  privKeyPEM= privKeyPEM.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("\n", "");
  // Remove the first and last lines
  privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");
  System.out.println(privKeyPEM);
  
  // Base64 decode the data
  byte [] encoded = Base64.decode(privKeyPEM);
  
  // PKCS8 decode the encoded RSA private key
  PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
  KeyFactory kf = KeyFactory.getInstance("RSA");
  PrivateKey privKey = kf.generatePrivate(keySpec);
  
  // Display the results
  System.out.println(privKey);
  </code></pre>
  
  <p>and it throws an <code>IOException : algid parse error, not a sequence</code>. Where is the error?</p>
  
  <pre><code>Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
  at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(Unknown Source)
  at java.security.KeyFactory.generatePrivate(Unknown Source)
  at base54.encrypt.RSAToy.main(RSAToy.java:36)
  Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
  at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
  at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
  at sun.security.rsa.RSAPrivateCrtKeyImpl.&lt;init&gt;(Unknown Source)
  at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(Unknown Source)
  at sun.security.rsa.RSAKeyFactory.generatePrivate(Unknown Source)
  </code></pre>