生成微信签名

/**  
    * 微信支付签名算法sign  
    * @param characterEncoding  
    * @param parameters  
    * @return  
    */    
   @Test   
   public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){    
       StringBuffer sb = 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)     
                   && !"sign".equals(k) && !"key".equals(k)) {    
               sb.append(k + "=" + v + "&");    
           }    
       }    
        
       sb.append("key=" + WeChatPayUtils.key);    
       System.out.println("字符串:"+sb.toString());  
       String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();    
       return sign;    
   }

 MD5Util工具类代码如下:

    package cn.cqzdkj.utils;  
      
    import java.security.MessageDigest;  
      
    public class MD5Util {  
        private static String byteArrayToHexString(byte b[]) {  
            StringBuffer resultSb = new StringBuffer();  
            for (int i = 0; i < b.length; i++)  
                resultSb.append(byteToHexString(b[i]));  
      
            return resultSb.toString();  
        }  
      
        private static String byteToHexString(byte b) {  
            int n = b;  
            if (n < 0)  
                n += 256;  
            int d1 = n / 16;  
            int d2 = n % 16;  
            return hexDigits[d1] + hexDigits[d2];  
        }  
      
        public static String MD5Encode(String origin, String charsetname) {  
            String resultString = null;  
            try {  
                resultString = new String(origin);  
                MessageDigest md = MessageDigest.getInstance("MD5");  
                if (charsetname == null || "".equals(charsetname))  
                    resultString = byteArrayToHexString(md.digest(resultString  
                            .getBytes()));  
                else  
                    resultString = byteArrayToHexString(md.digest(resultString  
                            .getBytes(charsetname)));  
            } catch (Exception exception) {  
            }  
            return resultString;  
        }  
      
        private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",  
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };  
    }  

然后在需要的地方直接调用上面的ctreasign这个方法就行了

    SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();    
          parameters.put("appid", appid);    
          parameters.put("mch_id", mch_id);  
          parameters.put("nonce_str",nonce_str);   
          parameters.put("body", body);   
          parameters.put("out_trade_no", "20170215");  
          parameters.put("total_fee", 1);  
          parameters.put("spbill_create_ip", "x.x.x.x");  
          parameters.put("notify_url","http://xxxxx.com");  
          parameters.put("trade_type", "JSAPI");  
          parameters.put("openid", "oGY_ZvxxxxxM");  
          parameters.put("sign","");  
            
           
          String characterEncoding = "UTF-8";    
          String mySign = createSign(characterEncoding,parameters);    
          System.out.println("我 的签名是:"+mySign);   

 转载:http://blog.csdn.net/qq_25821067/article/details/55253613

生成微信签名

上一篇:微信红包算法


下一篇:微信小程序制作下拉列表