code: public static String signWithRSA(String string, String keyString) throws GeneralSecurityException, IOException {
      try {
        RSAPrivateKey privateKey;
        // If PKCS#1
        if (keyString.contains(PKCS_1_KEY_START)) {
          byte[] bytes = parseDERFromPEM(keyString, PKCS_1_KEY_START, PKCS_1_KEY_END);
          privateKey = generatePrivateKeyFromPKCS10DER(bytes);
        } else {
          // else, assume PKCS#8
          byte[] bytes = parseDERFromPEM(keyString, P8_KEY_START, P8_KEY_END);
          privateKey = generatePrivateKeyFromPKCS8DER(bytes);
        }
  
        Signature rsa = Signature.getInstance("NONEwithRSA");
        rsa.initSign(privateKey);
        rsa.update(string.getBytes());
        byte[] signed = rsa.sign();
  
        return new String(Base64.getEncoder().encode(signed));
      } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
      }
    }
docs: Sign the provided string with the private key and the provided signature. The provided private key string is expected to be in one of
  the two formats: PKCS#1 or PKCS#8.
  
  @param string    The string to sign.
  @param keyString The private to use when signing the string.
  @return the signed string.