/** * @throws Exception * <p>Description:解密URL请求的参数<br> * @author 胡建盼 * @version 0.1 2014年10月15日 * @param map void * @throws */ public static Map<String, String> buildDecryptionParameter( Map<String, String> map, String askMode, String timestamp) throws Exception { Map<String, String> returnMap = new TreeMap<String, String>(); StringBuilder paramsUncoded = new StringBuilder(map.size() * 16); StringBuilder paramsDecoded = new StringBuilder(map.size() * 16); returnMap.put("timestamp", timestamp); for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); // sign参数放在最后 if (!"sign".equals(key) && !"timestamp".equals(key)) { String value = null; if (TZJConstants.GET_URL.equals(askMode)) { // GET 请求 value = DES3.decryptModeTzj( new String(((entry.getValue()).getBytes(EncapUrlParameter.UTF_8_CHARSET))), timestamp); } else if (TZJConstants.POST_URL.equals(askMode)) { value = DES3.decryptModeTzj( new String((entry.getValue().getBytes(EncapUrlParameter.UTF_8_CHARSET))), timestamp); } paramsUncoded.append(key).append("=").append(value).append("&"); paramsDecoded.append(key).append("=").append(value).append("&"); returnMap.put(key, value); } } /** 计算MD5值 */ String src = new String( paramsUncoded .toString() .substring(0, paramsUncoded.toString().length() - 1) .getBytes(UTF_8_CHARSET), UTF_8_CHARSET); String paramDM5 = EncryptMD5.toMD5(src); paramsDecoded.append("sign").append("=").append(paramDM5); // 校验MD5值是否正确 if (paramDM5.equals(map.get("sign"))) { returnMap.put("sign", paramDM5); } else { returnMap.put("sign", null); } return returnMap; }
/** * Description:构造URL请求的加密参数<br> * * @author 胡建盼 * @version 0.1 2014年10月15日 * @param map * @return * @throws Exception String */ public static String buildEncryptParameter( Map<String, String> map, String askMode, String timestamp) throws Exception { StringBuilder paramsUncoded = new StringBuilder(map.size() * 16); StringBuilder paramsEncoded = new StringBuilder(map.size() * 16); for (Map.Entry<String, String> entry : map.entrySet()) { // sign参数放在最后 if (!"sign".equals(entry.getKey()) && !"timestamp".equals(entry.getKey())) { paramsUncoded.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); if (TZJConstants.GET_URL.equals(askMode)) { // GET 请求 paramsEncoded .append(entry.getKey()) .append("=") // .append( URLEncoder.encode( new String( (DES3.encryptModeTzj(entry.getValue(), timestamp) .getBytes(EncapUrlParameter.UTF_8_CHARSET))), EncapUrlParameter.UTF_8_CHARSET)) .append("&"); } else if (TZJConstants.POST_URL.equals(askMode)) { // POST 请求 paramsEncoded .append(entry.getKey()) .append("=") .append( new String( (DES3.encryptModeTzj(entry.getValue(), timestamp) .getBytes(EncapUrlParameter.UTF_8_CHARSET)))) .append("&"); } } } final String substring = new String( paramsUncoded .toString() .substring(0, paramsUncoded.toString().length() - 1) .getBytes(UTF_8_CHARSET), UTF_8_CHARSET); paramsEncoded.append("sign").append("=").append(EncryptMD5.toMD5(substring)); paramsEncoded.append("timestamp").append("=").append(timestamp); System.out.println(paramsEncoded.toString()); return paramsEncoded.toString(); }