public static PrivateKey getPrivateKey(String privateKeyFilepath,
      String algorithm) throws SamlException {
    try {
      InputStream privKey = null;
      try {
        URL url = new URL(privateKeyFilepath);
        privKey = url.openStream();
      } catch (MalformedURLException e) {
        privKey = new FileInputStream(privateKeyFilepath);
      }
      byte[] bytes = new byte[privKey.available()];
      privKey.read(bytes);
      privKey.close();
      PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(bytes);
      KeyFactory factory = KeyFactory.getInstance(algorithm);
      return factory.generatePrivate(privSpec);
    } catch (FileNotFoundException e) {
      throw new SamlException("ERROR: Private key file not found - "
        + privateKeyFilepath);
    } catch (IOException e) {
      throw new SamlException("ERROR: Invalid private key file - "
        + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
      throw new SamlException("ERROR: Invalid private key algorithm - "
        + e.getMessage());
    } catch (InvalidKeySpecException e) {
      throw new SamlException("ERROR: Invalid private key spec - "
        + e.getMessage());
    }
  }