title: Algid parse error, not a sequence
body: <p>When trying to read a RSA private key from a file using the method</p>
  
  <pre><code>public PrivateKey getPrivateKey()
          throws NoSuchAlgorithmException,
          InvalidKeySpecException, IOException {
  
      final InputStream inputStream = getClass().getClassLoader()
                      .getResourceAsStream("privatekey");
      byte[] privKeyBytes = null;
      try {
          privKeyBytes = IOUtils.toByteArray(inputStream);
      } catch (final IOException exception) {
          LOGGER.error("", exception);
          IOUtils.closeQuietly(inputStream);
      }
  
      LOGGER.debug("privKeyBytes: {}", privKeyBytes);
  
      String BEGIN = "-----BEGIN RSA PRIVATE KEY-----";
      String END = "-----END RSA PRIVATE KEY-----";
      String str = new String(privKeyBytes);
      if (str.contains(BEGIN) &amp;&amp; str.contains(END)) {
          str = str.substring(BEGIN.length(), str.lastIndexOf(END));
      }
  
      KeyFactory fac = KeyFactory.getInstance("RSA");
      EncodedKeySpec privKeySpec =
              new PKCS8EncodedKeySpec(Base64.decode(str.getBytes()));
      return fac.generatePrivate(privKeySpec);
  }
  </code></pre>
  
  <p>I get the exception</p>
  
  <pre><code>java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
      at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:200) ~[na:1.6.0_23]
      at java.security.KeyFactory.generatePrivate(KeyFactory.java:342) ~[na:1.6.0_23]
  </code></pre>
  
  <p>at the fac.generatePrivate(privKeySpec) call.</p>
  
  <p>What does this error mean?</p>
  
  <p>Thanks</p>
  
  <p>Dmitri</p>