求java实现微信签名 财富值93

2016-10-22 10:02发布

1.求java怎么实现微信签名

package sign;  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" };    }  package sign;  import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap;  public class pay {          //http://mch.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3       private static String Key = "1394290902";          /**       * @param args       */       public static void main(String[] args) {           System.out.println(">>>模拟微信支付<<<");           System.out.println("==========华丽的分隔符==========");           //微信api提供的参数           String appid = "wx9f25641641562bde";           String mch_id = "1394290902";           String nonce_str = "3PG2J4ILTKCH16CQ2502SI8ZNMTM67VS";                      SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();           parameters.put("appid", appid);           parameters.put("mch_id", mch_id);           parameters.put("nonce_str", nonce_str);                      String characterEncoding = "UTF-8";           String weixinApiSign = "9A0A8659F005D6984697E2CA0A9CF3B7";           System.out.println("微信的签名是:" + weixinApiSign);           String mySign = createSign(characterEncoding,parameters);           System.out.println("我     的签名是:"+mySign);                      if(weixinApiSign.equals(mySign)){               System.out.println("恭喜你成功了~");           }else{               System.out.println("注定了你是个失败者~");           }                      String userAgent = "Mozilla/5.0(iphone;CPU iphone OS 5_1_1 like Mac OS X) AppleWebKit/534.46(KHTML,like Geocko) Mobile/9B206 MicroMessenger/5.0";                      char agent = userAgent.charAt(userAgent.indexOf("MicroMessenger")+15);                      System.out.println("微信的版本号:"+new String(new char[]{agent}));       }          /**       * 微信支付签名算法sign       * @param characterEncoding       * @param parameters       * @return       */       @SuppressWarnings("unchecked")       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=" + Key);           String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();           return sign;       }    }

为什么用这个生成的签名是错误的
appid也没有错啊

友情提示: 问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
0条回答

一周热门 更多>