MD5 是常用的加密算法,是不可逆的。既只能加密,但不能解密。
package cn.com.ctsi.csdp.base.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Md5Tool { private static Logger logger = LoggerFactory.getLogger(Md5Tool.class); /** * @param str * @return */ public static String getMD5Hex(String str) { MessageDigest md = null; StringBuffer sb = new StringBuffer(); byte[] tmpByte = null; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { logger.info("NoSuchAlgorithmException" + e); e.printStackTrace(); } md.update(str.getBytes()); tmpByte = md.digest(); for (int i = 0; i < tmpByte.length; ++i) { if ((tmpByte[i] & 0xFF) < 16) { sb.append("0"); } sb.append(Long.toString(tmpByte[i] & 0xFF, 16)); } return sb.toString(); } }