前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

首先MD5其实就是一种hash,或者叫散列函数,有的地方叫杂凑函数,都是一个东西,其实它就是一种映射,而平常最常见的就是说MD5是不可逆的,为什么不可逆呢?

考虑MD5是多对一的映射,也就是说很多不同的经过MD5变换之后可能会是相同的,那么既然多对一,自然是不可逆啦,你怎么会知道他到底是由哪个变换过来的呢

一个字符串通过md5执行,不论执行多少次都是相同的



package com.hlj.proj.utils;

import java.security.MessageDigest;

/**
 * MD5加密类
 */
public class MD5Util {

	public static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(MD5Util.class);

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };


	public static void main(String[] args) {

		String name =  "HealerJean" ;
		System.out.println(MD5Encode(name));
	}



	/**
	 * MD5编码
	 *
	 * @param origin    原始字符串
	 * @return 经过MD5加密之后的结果
	 */
	public static String MD5Encode(String origin) {
		String resultString = null;
		try {
			resultString = origin;
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString.getBytes("UTF-8")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultString;
	}



	/**
	 * 转换字节数组为16进制字串
	 * @param b  字节数组
	 * @return 16进制字串
	 */
	public static String byteArrayToHexString(byte[] b) {
		StringBuilder resultSb = new StringBuilder();
		for (byte aB : b) {
			resultSb.append(byteToHexString(aB));
		}
		return resultSb.toString();
	}

	/**
	 * 转换byte到16进制
	 * @param b   要转换的byte
	 * @return 16进制格式
	 */
	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0) {
			n = 256 + n;
		}
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}






}


ContactAuthor