1. 程式人生 > >MD5加密中文請求引數亂碼問題解決

MD5加密中文請求引數亂碼問題解決

public static String md5(String str,String charsetName) {
   String result = "";
   MessageDigest md5 = null;
   try {
      md5 = MessageDigest.getInstance("MD5");
      md5.update(str.getBytes(charsetName));
   } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
   }
   byte 
b[] = md5.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } result = buf.toString(); return
result; }