md5得到16或32位十六進位制
阿新 • • 發佈:2019-02-07
public static String 32Md5(String text) { StringBuffer buf = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(text.getBytes()); byte b[] = md.digest(); int i; 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)); }} catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } returnbuf.toString(); }
public static String 16Md5(String text) { StringBuffer buf = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(text.getBytes()); byte b[] = md.digest(); int i; 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)); }} catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return buf.toString().substring(8,24);
}