@Test public void keyPemStringToRsaPublicKey__withX509KeyPemStringWithoutLineSeparators__successfullyGeneratesPublicKey() throws Exception { String publicKeyStringWithoutLineSeparators = "-----BEGIN PUBLIC KEY-----" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvcN6NPvcBXSDa0WxmFQj" + "LJZQaYBcw60vBIn7KOmK++qTz6GPtci1AOzv67uxlG5urWFSSCiaNqGu8jN0HheF" + "PPmz35HLgqj+UuNxXKXy82RBCOrPrhOODoP6YD4Bj065NXUPEl0KHEBDrgEulhfl" + "MxhuxiaXcJb0dlDTO5Lp2e8KwgCmLu8iJuHzoOLomz/FjF0tx0qdjbNAqw7uRqyd" + "2Hz/e1RIhddiy45H0oElcFemPLfWlgSxKNFsZK0yEws/gtwhcjztXfEPbVmq0Les" + "wZ8A+tAEPVk+vFHqHfxy9XHhkF4RN4f7+/6qU/Ou+ObVPDEAgJPDa+clDTqrjKGo" + "fwIDAQAB" + "-----END PUBLIC KEY-----"; PublicKey publicKey = PopTokenValidatorUtils.keyPemStringToRsaPublicKey(publicKeyStringWithoutLineSeparators); assertNotNull("Public key should not be null", publicKey); }
public static RSAPublicKey keyPemStringToRsaPublicKey(String publicKeyPemString) throws PopPublicKeyParseException { if (StringUtils.isBlank(publicKeyPemString)) { throw new IllegalArgumentException("The publicKeyPemString should not be null or empty"); } try { if (publicKeyPemString.startsWith(X509_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(X509_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(X509_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(X509_PUBLIC_KEY_SUFFIX)); publicKeyPemString = sanitizePublicKeyPemString(publicKeyPemString); byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else if (publicKeyPemString.startsWith(PKCS_1_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(PKCS_1_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(PKCS_1_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(PKCS_1_PUBLIC_KEY_SUFFIX)); publicKeyPemString = RSA_ENCRYPTION_ALGORITHM_IDENTIFIER + publicKeyPemString; byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else { throw new PopPublicKeyParseException("The publicKeyPemString contains unsupported format"); } } catch (PopPublicKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPublicKeyParseException( "Error occurred while converting publicKeyPemString to RSAPublicKey, error: " + ex.toString(), ex); } }
PopTokenValidatorUtils { public static RSAPublicKey keyPemStringToRsaPublicKey(String publicKeyPemString) throws PopPublicKeyParseException { if (StringUtils.isBlank(publicKeyPemString)) { throw new IllegalArgumentException("The publicKeyPemString should not be null or empty"); } try { if (publicKeyPemString.startsWith(X509_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(X509_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(X509_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(X509_PUBLIC_KEY_SUFFIX)); publicKeyPemString = sanitizePublicKeyPemString(publicKeyPemString); byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else if (publicKeyPemString.startsWith(PKCS_1_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(PKCS_1_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(PKCS_1_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(PKCS_1_PUBLIC_KEY_SUFFIX)); publicKeyPemString = RSA_ENCRYPTION_ALGORITHM_IDENTIFIER + publicKeyPemString; byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else { throw new PopPublicKeyParseException("The publicKeyPemString contains unsupported format"); } } catch (PopPublicKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPublicKeyParseException( "Error occurred while converting publicKeyPemString to RSAPublicKey, error: " + ex.toString(), ex); } } }
PopTokenValidatorUtils { public static RSAPublicKey keyPemStringToRsaPublicKey(String publicKeyPemString) throws PopPublicKeyParseException { if (StringUtils.isBlank(publicKeyPemString)) { throw new IllegalArgumentException("The publicKeyPemString should not be null or empty"); } try { if (publicKeyPemString.startsWith(X509_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(X509_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(X509_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(X509_PUBLIC_KEY_SUFFIX)); publicKeyPemString = sanitizePublicKeyPemString(publicKeyPemString); byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else if (publicKeyPemString.startsWith(PKCS_1_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(PKCS_1_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(PKCS_1_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(PKCS_1_PUBLIC_KEY_SUFFIX)); publicKeyPemString = RSA_ENCRYPTION_ALGORITHM_IDENTIFIER + publicKeyPemString; byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else { throw new PopPublicKeyParseException("The publicKeyPemString contains unsupported format"); } } catch (PopPublicKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPublicKeyParseException( "Error occurred while converting publicKeyPemString to RSAPublicKey, error: " + ex.toString(), ex); } }  }
PopTokenValidatorUtils { public static RSAPublicKey keyPemStringToRsaPublicKey(String publicKeyPemString) throws PopPublicKeyParseException { if (StringUtils.isBlank(publicKeyPemString)) { throw new IllegalArgumentException("The publicKeyPemString should not be null or empty"); } try { if (publicKeyPemString.startsWith(X509_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(X509_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(X509_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(X509_PUBLIC_KEY_SUFFIX)); publicKeyPemString = sanitizePublicKeyPemString(publicKeyPemString); byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else if (publicKeyPemString.startsWith(PKCS_1_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(PKCS_1_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(PKCS_1_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(PKCS_1_PUBLIC_KEY_SUFFIX)); publicKeyPemString = RSA_ENCRYPTION_ALGORITHM_IDENTIFIER + publicKeyPemString; byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else { throw new PopPublicKeyParseException("The publicKeyPemString contains unsupported format"); } } catch (PopPublicKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPublicKeyParseException( "Error occurred while converting publicKeyPemString to RSAPublicKey, error: " + ex.toString(), ex); } }  static RSAPublicKey keyPemStringToRsaPublicKey(String publicKeyPemString); static RSAPublicKey jwkStringToRsaPublicKey(String jwkString); static String sha256Base64UrlSafe(String stringToHash); }
PopTokenValidatorUtils { public static RSAPublicKey keyPemStringToRsaPublicKey(String publicKeyPemString) throws PopPublicKeyParseException { if (StringUtils.isBlank(publicKeyPemString)) { throw new IllegalArgumentException("The publicKeyPemString should not be null or empty"); } try { if (publicKeyPemString.startsWith(X509_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(X509_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(X509_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(X509_PUBLIC_KEY_SUFFIX)); publicKeyPemString = sanitizePublicKeyPemString(publicKeyPemString); byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else if (publicKeyPemString.startsWith(PKCS_1_PUBLIC_KEY_PREFIX) && publicKeyPemString.contains(PKCS_1_PUBLIC_KEY_SUFFIX)) { publicKeyPemString = publicKeyPemString.substring(PKCS_1_PUBLIC_KEY_PREFIX.length(), publicKeyPemString.indexOf(PKCS_1_PUBLIC_KEY_SUFFIX)); publicKeyPemString = RSA_ENCRYPTION_ALGORITHM_IDENTIFIER + publicKeyPemString; byte[] keyBytes = toBase64DecodedBytes(publicKeyPemString); return (RSAPublicKey) buildPublicKeyFromX509KeyBytes(keyBytes); } else { throw new PopPublicKeyParseException("The publicKeyPemString contains unsupported format"); } } catch (PopPublicKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPublicKeyParseException( "Error occurred while converting publicKeyPemString to RSAPublicKey, error: " + ex.toString(), ex); } }  static RSAPublicKey keyPemStringToRsaPublicKey(String publicKeyPemString); static RSAPublicKey jwkStringToRsaPublicKey(String jwkString); static String sha256Base64UrlSafe(String stringToHash);  }