欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

签名生成 参数名ASCII码从小到大排序

程序员文章站 2022-05-28 09:27:14
...
public static void main(String[] args) {
       SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
       //String mfrchant_id="fffff";
       String merchant_id="190010002";
       String business_type="1005";
       String out_trade_no="1400000001";
       String key="3A4BC4A4000CF1B5FFA9E351E6C1539E";
       //parameters.put("mfrchant_id", mfrchant_id);
       parameters.put("merchant_id", merchant_id);
       parameters.put("business_type", business_type);
       parameters.put("out_trade_no",out_trade_no);
       String characterEncoding = "UTF-8";         //指定字符集UTF-8
       String mySign = createSign(characterEncoding,parameters,key);
       //System.out.println("我 的签名是:"+mySign);
   }
public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters,String key){
    StringBuffer sb = new StringBuffer();
    StringBuffer sbkey = new StringBuffer();
    Set es = parameters.entrySet();  //所有参与传参的参数按照accsii排序(升序)
    Iterator it = es.iterator();
    while(it.hasNext()) {
        Map.Entry entry = (Map.Entry)it.next();
        String k = (String)entry.getKey();
        Object v = entry.getValue();
        //空值不传递,不参与签名组串
        if(null != v && !"".equals(v)) {
            sb.append(k + "=" + v + "&");
            sbkey.append(k + "=" + v + "&");
        }
    }
    //System.out.println("字符串:"+sb.toString());
    sbkey=sbkey.append("key="+key);
    System.out.println("字符串:"+sbkey.toString());
    //MD5加密,结果转换为大写字符
    String sign = MD5Util.MD5Encode(sbkey.toString(), characterEncoding).toUpperCase();
    System.out.println("MD5加密值:"+sign);
    return sb.toString()+"sign="+sign;
}
/*
---------------------
作者:chenghaibing2008
来源:CSDN
原文:https://blog.csdn.net/chenghaibing2008/article/details/79701773
版权声明:本文为博主原创文章,转载请附上博文链接!

签名生成 参数名ASCII码从小到大排序
*/