title: How to parse in memory string to get private and public key RSA
body: <p>I am able to read the public and private key from the file directly as file stream convert that to PublicKey and PrivateKey Object. but If load as string it fails. </p>
  
  <p>Working code </p>
  
  <pre><code>    public static RSAPrivateKey getPrivateKey(String filename) throws IOException, GeneralSecurityException {
  
              PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
                      IOUtils.toByteArray(new FileInputStream(filename)));
              return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec);
          }
  
          public static RSAPublicKey getPublicKey(String filename) throws IOException, GeneralSecurityException {
              X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
                      IOUtils.toByteArray(new FileInputStream(filename)));
              return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);
          }
  
  public static void main(String[] args) throws JOSEException, ParseException, IOException, GeneralSecurityException {
  
          String publicKeyPathName = "../public";
          String privateKeyPathName = "../private";
          /// loading from file
           RSAPublicKey publicKey = new MYClass().getPublicKey(publicKeyPathName);
           RSAPrivateKey privateKey = new MYClass().getPrivateKey(privateKeyPathName);
  }
  </code></pre>
  
  <p>Not Working code </p>
  
  <pre><code>public static RSAPrivateKey parsePrivateKey(String content) throws IOException, GeneralSecurityException {
          content = content.replaceAll("\\n", "").replace("-----BEGIN RSA PRIVATE KEY-----", "")
                  .replace("-----END RSA PRIVATE KEY-----", "");
          PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(content.getBytes(StandardCharsets.UTF_8));
          return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec);
      }
  
      public static RSAPublicKey parsePublicKey(String content) throws IOException, GeneralSecurityException {
          content = content.replaceAll("\\n", "").replace("-----BEGIN PUBLIC KEY-----", "")
                  .replace("-----END PUBLIC KEY-----", "");
          X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(content.getBytes(StandardCharsets.UTF_8));
          return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);
      }
  public String getPublicKey() {
          return "-----BEGIN PUBLIC KEY-----\n" 
                  + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAswPxKZWJEQ0RVtiaoFIU"
                  + "jndalEp2sZF87Cy9IxW1FxxcfDmZRlkMDe+5X1lzpOjFHP8mBZa8vhMjSKbJY2wt"
                  + "MyxckbBoTjAIefi34KuD0Q6j9LRt1r1iQ+nMYezk5N83ruqyRHtB4a8AOkw3iE2z"
                  + "zA74uAkCzPiYUrAES1I9mVbpxJZf2Ej0px6drkd4OQ2aPnwR13gWTnmqFWV+Xkl6"
                  + "WapMArewaAUXU9zDqjn4ZpNCkgx6m4CK7PDt+VgYUi+sjI0WzPIb0FMDTYSe1iX6"
                  + "mfIabvoetxUmtyQgu4RfHIiOF0no6VEPh31of2B+KZX2OjHKjyKBGUxUkDLm+J2J" 
                  + "CwIDAQAB\n"
                  + "-----END PUBLIC KEY-----";
      }
  
      public String getPrivateKey() {
          return "-----BEGIN RSA PRIVATE KEY-----\n"
                  + "MIIEowIBAAKCAQEAswPxKZWJEQ0RVtiaoFIUjndalEp2sZF87Cy9IxW1FxxcfDmZ"
                  + "RlkMDe+5X1lzpOjFHP8mBZa8vhMjSKbJY2wtMyxckbBoTjAIefi34KuD0Q6j9LRt"
  + "1r1iQ+nMYezk5N83ruqyRHtB4a8AOkw3iE2zzA74uAkCzPiYUrAES1I9mVbpxJZf"
  + "2Ej0px6drkd4OQ2aPnwR13gWTnmqFWV+Xkl6WapMArewaAUXU9zDqjn4ZpNCkgx6"
  + "m4CK7PDt+VgYUi+sjI0WzPIb0FMDTYSe1iX6mfIabvoetxUmtyQgu4RfHIiOF0no"
  + "6VEPh31of2B+KZX2OjHKjyKBGUxUkDLm+J2JCwIDAQABAoIBAFj59p4SoyQEeL/P"
  + "vVs+MNV24WCfektb4tmIiyGrIm0wPY/JGTPsAp/SrTKFRYOL5p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VKuzwEfP7NgeLZdtqjiZBCZiLoP8SRm68FJS"
  + "2UpGX4K7rQhx66H6UwXHylBfush56Z8Y9g7PYel83sy+4bqjod1a5TqNBJMkhDQi"
  + "DrQmDIfpFoRYcOyB4+BeF/w7wL+R5ofov4T+0vB3ST+aztphp2G4\n"
  + "-----END RSA PRIVATE KEY-----";
      }
  
  public static void main(String[] args) throws JOSEException, ParseException, IOException, GeneralSecurityException {
  
   String publicKeyString = new MyClass().getPublicKey();
           RSAPublicKey publicKey = new MyClass().parsePublicKey(publicKeyString);
  
           String privateKeyString = new MyClass().getPrivateKey();
           RSAPrivateKey privateKey = new
           MyClass().parsePrivateKey(privateKeyString);
  
  }
  </code></pre>
  
  <p>I tried without removing the first line, and try to read the file as string . none of them or working. tell me the correct way to use this.</p>
  
  <p>The error I am getting is </p>
  
  <blockquote>
    <p>Exception in thread "main" java.security.spec.InvalidKeySpecException:
    java.security.InvalidKeyException: invalid key format     at
    sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:205)
      at java.security.KeyFactory.generatePublic(KeyFactory.java:334)     at
    j.MyClass.parsePublicKey(MyClass.java:136)    at
    j.MyClass.main(MyClass.java:223) Caused by:
    java.security.InvalidKeyException: invalid key format     at
    sun.security.x509.X509Key.decode(X509Key.java:387)    at
    sun.security.x509.X509Key.decode(X509Key.java:403)    at
    sun.security.rsa.RSAPublicKeyImpl.(RSAPublicKeyImpl.java:84)    at
    sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:298)
      at
    sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:201)
      ... 3 more</p>
  </blockquote>
  
  <p>My usecase is, </p>
  
  <p>read a keys from the db and do the conversion. </p>