1. 程式人生 > >Java生成十六進位制的MD5加密字串

Java生成十六進位制的MD5加密字串

package com.zzj;

import java.math.BigInteger;
import java.security.MessageDigest;


public class MD5ToHexString {
	public static void main(String[] args) throws Exception {
		byte[] bs = "I love you!I love you!I love you!".getBytes("UTF-8");
		System.out.println(md5(bs));
	}
	
	public static String md5(byte[] bs) throws Exception{
		MessageDigest digest = MessageDigest.getInstance("MD5");
		digest.update(bs);
		String hex = new BigInteger(1, digest.digest()).toString(16);
		// 補齊BigInteger省略的前置0
		return new String(new char[32 - hex.length()]).replace("\0", "0") + hex;
	}

}