第一步: public static void main(String[] args) throws Exception { String src = "abc"; byte[] bytes = src.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] targ =md.digest(bytes); System.out.println(targ.length); } 第二步: public static void main(String[] args) throws Exception { StringBuffer buffer = new StringBuffer(); char[] chars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; String src = "abc"; byte[] bytes = src.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] targ =md.digest(bytes); for(byte b: targ){ buffer.append(chars[(b>>4) & 0x0f]); buffer.append(chars[b & 0x0f]); } System.out.println(buffer.toString()); } 第三步: /** * 数据 */ public class DataUtil { /** * 使用md5算法进行加密 */ public static String md5(String src){ try { StringBuffer buffer = new StringBuffer(); char[] chars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; byte[] bytes = src.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] targ = md.digest(bytes); for(byte b: targ){ buffer.append(chars[(b >> 4) & 0x0F]); buffer.append(chars[b & 0x0F]); } return buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return null ; } } |
|