class JwkToPublicKeyConverter {

    String convertFirstPublicKeyJwkToPublicKeyPem(String jwkJson) throws JOSEException, ParseException {
        PublicKey key = JWKSet.parse(jwkJson).toPublicJWKSet().getKeys().get(0).toRSAKey().toPublicKey();
        String encoded = Base64.getEncoder().encodeToString(key.getEncoded());
        StringBuilder s = new StringBuilder();
        s.append("-----BEGIN PUBLIC KEY-----");
        for (int i = 0; i < encoded.length(); i++) {
            if (((i % 64) == 0) && (i != (encoded.length() - 1))) {
                s.append("\n");
            }
            s.append(encoded.charAt(i));
        }
        s.append("\n");
        s.append("-----END PUBLIC KEY-----\n");
        return s.toString();
    }

}