我正在尝试从webView上的卡付款。我从html网站尝试了相同的值,但没有问题,但是我从webView尝试总是说“哈希错误!”。我认为编码类型可能有所不同。你有什么想法我很累。我已经为此工作了很长时间。请帮我。

    String postData = null;
    try {
        String plaintext = clientid + oid + amount + okUrl + failUrl + islemtipi + taksit + rnd + sanal_pos_key;
        String hash = toSHA1(plaintext.getBytes());
        postData = "clientid=" + URLEncoder.encode(clientid, "UTF-8")
                + "&storetype=" + URLEncoder.encode(storetype, "UTF-8")
                + "&islemtipi=" + URLEncoder.encode(islemtipi, "UTF-8")
                + "&amount=" + URLEncoder.encode(amount, "UTF-8")
                + "&oid=" + URLEncoder.encode(oid, "UTF-8")
                + "&pan=" + URLEncoder.encode(pan, "UTF-8")
                + "&cv2=" + URLEncoder.encode(cv2, "UTF-8")
                + "&Ecom_Payment_Card_ExpDate_Year=" + URLEncoder.encode(Ecom_Payment_Card_ExpDate_Year, "UTF-8")
                + "&Ecom_Payment_Card_ExpDate_Month=" + URLEncoder.encode(Ecom_Payment_Card_ExpDate_Month, "UTF-8")
                + "&cardType=" + URLEncoder.encode(cardType, "UTF-8")
                + "&firmaadi=" + URLEncoder.encode(firmaadi, "UTF-8")
                + "&taksit=" + URLEncoder.encode(taksit, "UTF-8")
                + "&okUrl=" + URLEncoder.encode(okUrl, "UTF-8")
                + "&failUrl=" + URLEncoder.encode(failUrl, "UTF-8")
                + "&rnd=" + URLEncoder.encode(rnd, "UTF-8")
                + "&hash=" + URLEncoder.encode(hash, "UTF-8");

        Log.d("plaintext: ", plaintext);
        Log.d("hash: ", hash);
        Log.d("rnd: ", rnd);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (postData != null) {
        webView.postUrl(url, postData.getBytes());
        Log.d("Web View URL: ", webView.getUrl());
        webView.setVisibility(View.VISIBLE);
        layout.setVisibility(View.GONE);
    }

public static String toSHA1(byte[] convertme) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString((md.digest(convertme)),Base64.DEFAULT);
}

最佳答案

该代码有效。

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    String result;
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    byte[] sha1hash = md.digest();
    result = Base64.encodeToString(sha1hash, Base64.DEFAULT);
    result = result.substring(0, result.length()-1);
    return result;
}

09-09 22:51