title: java.security.InvalidKeyException: invalid key format ... PublicKey error
body: <p>i know there are multiple questions, but none have seem to adapt to what i need.</p>
  <p>I need to validate a JWT token, i only have the PublicKey (which is fine as with that i can validate the sign of jwt.</p>
  <p>Code is as follow.</p>
  <p>First we have publicKey</p>
  <pre><code>private static final String publicKey = &quot;MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA88aLoh5l9W9UY3Hb+YGU\r\n&quot;
              + &quot;ZQBwVWuNTNpF5nm9uU+MqmB7EUmTYdD4Jk09zjBUutnggY10Jbjxdrv09HnHrqbj\r\n&quot;
              + &quot;+hyOyIXgSsVIK5bGZVHKLJzzZmWgn0QgcEXzR97J47MFeWkn0tEgDlP8LEMZE7ix\r\n&quot;
              + &quot;9WgoJHGGtUaQV5MM3U9S13zyRmaYekCVfLh2REHevb1aHgDReCLx92ZIdc9ldE9g\r\n&quot;
              + &quot;99v87E5zNKSv3AI8EVRt/Tpjfyuk7XTEqY6pz83tCfy1uch8NihhVjY8O2J3pilP\r\n&quot;
              + &quot;VW/L83GMJ5Shea0mE2Dq9Gh4lrVvAy+OHkarwGgdlYLwy1Dmof2SYCCq2SJyv5ZR\r\n&quot;
              + &quot;fwIDAQAB&quot;;
  </code></pre>
  <p>Then inside a method we got</p>
  <pre><code>JWTVerifier verifierToken =  JWT.require(Algorithm.RSA256((RSAPublicKey) getPublicFederaKey(), null)).build();
          DecodedJWT decodeJwt = verifierToken.verify(token);
          Map&lt;String, Object&gt; claims = new HashMap&lt;String, Object&gt;();
          
          String codiceFiscale = decodeJwt.getClaim(&quot;CODICEFISCALE&quot;).asString();
          System.err.println(&quot;CODICE FISCALE: &quot;+codiceFiscale);
          
          if (decodeJwt.getExpiresAt().before(java.util.Calendar.getInstance().getTime())) {
                throw new RuntimeException(&quot;Expired token!&quot;);
          }
  </code></pre>
  <p>If you see on verifiedToken it makes reference to getPublicFederaKey();</p>
  <pre><code>private PublicKey getPublicFederaKey() throws CustomException {
          PublicKey key = null;
          
  
          try{
              
          String keyTrimmed = publicKey.trim().replace(&quot;\n&quot;, &quot;&quot;).replace(&quot;\r&quot;, &quot;&quot;);
          System.err.println(keyTrimmed);
          int stringLength = keyTrimmed.length(); //392
          String hexString = Hex.encodeHexString(publicKey.getBytes(&quot;UTF-8&quot;));
          byte[] hexedPublicKey = DatatypeConverter.parseHexBinary(hexString);
          X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(hexedPublicKey);
  
          KeyFactory kf = KeyFactory.getInstance(&quot;RSA&quot;);
  
          key = kf.generatePublic(keySpecX509);
          } catch (Exception e) {
          throw new CustomException(e.getMessage());
          }
          return key;
          }
  </code></pre>
  <p>I saw an answer here and tried the hexParsing directly with the String but gave me error of illegal character present for hex or sth like that (i think its because it has a-f characters) so i guessed ok, its because it is waiting for a hex instead of raw String.</p>
  <p>Then before i tried with Base64 something similar to this</p>
  <pre><code>rsa = Cipher.getInstance(&quot;RSA&quot;);
  KeyFactory keyFactory = KeyFactory.getInstance(&quot;RSA&quot;);
  X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pkey.getBytes());
  PublicKey pk = keyFactory.generatePublic(publicKeySpec);
  </code></pre>
  <p>Then at the end as you can see i am triying to replicate &quot;Duncan Jones&quot; response to no avail.</p>
  <p><a href="https://stackoverflow.com/questions/25622894/java-security-invalidkeyexception-invalid-key-format-on-generating-rsa-public-k">java.security.InvalidKeyException: invalid key format on generating RSA public key</a></p>
  <p>In any case what i need is the String publicKey (i cut the ----BEGIN---- -----END----), to be transformed to java.security.PublicKey to then be cast to java.security.interfaces.RSAPublicKey</p>
  <p>Thank you very much in advance.</p>