1 package com.Md5; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 6 public class Md5JdkUtil { 7 /** 8 * 将byte数组转化为16进制输出 9 * @param bytes 10 * @author Administrator 11 * @return String 12 */ 13 public static String convertByteToHexString(byte[] bytes) { 14 String result = ""; 15 for(int i=0;i<bytes.length;i++) { 16 int temp = bytes[i] & 0xff; 17 String tempHex = Integer.toHexString(temp); 18 if(tempHex.length() < 2) { 19 result += "0" + tempHex; 20 }else { 21 result += tempHex; 22 } 23 } 24 25 return result; 26 } 27 /** 28 * jdk自带的Md5加密 29 * @param message 30 * @return String 31 */ 32 public static String Md5Jdk(String message) { 33 String temp = ""; 34 try { 35 MessageDigest md5Digest = MessageDigest.getInstance("MD5"); 36 byte[] encodeMd5Digest = md5Digest.digest(message.getBytes()); 37 //将encodeMd5Digest字节数组转化为16进制 38 temp = convertByteToHexString(encodeMd5Digest); 39 } catch (NoSuchAlgorithmException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 44 return temp; 45 } 46 47 public static void main(String[] args) { 48 //Md5加密算法 49 String md5Encode = Md5Jdk("951755155"); 50 System.out.println("Md5JDK加密后为:" + md5Encode); 51 System.out.println("Md5JDK加密后密文的长度为:" + md5Encode.length()); 52 } 53 }