MD5加密示例

在日常的工作中,经常会用到加密,今天写了一个简单地运用MD5加密的示例,现在贴上源码.

MD5Utils类

package com.dengsilinming.test;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author dengsilinming
 * @date 2012-12-27 上午10:49:16
 *
 */
public class MD5Utils {

    private static final String MD5_SUFFIX = "_dengsilinming";
    
    /**
     * 字符加密
     * @param str 明文
     * @return 返回加了后缀的加密字符
     */
    public static String encryptByMD5(String str) {
        try {
            if (str == null || str.length() < 1 || "0".equals(str))
                str = "0";
            String tmp = md5(str + MD5_SUFFIX, "UTF-8");
           if (null != tmp) {
               return replace(tmp, ":", "", -1).toLowerCase();
           }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    /**
     * md5加密方法,不可逆转
     * @param str 明文
     * @param charset 字符编码
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String md5(String str, String charset) 
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] tmpInput = null;
        if (null != str) {
            if (null != charset) {
                tmpInput = str.getBytes(charset);
            } else {
                tmpInput = str.getBytes();
            }
        } else {
            return null;
        }
        MessageDigest alg = MessageDigest.getInstance("MD5"); // or "SHA-1"
        alg.update(tmpInput);
        return byte1hex(alg.digest());
    }
    
    /**
     * 字节码转换成16进制字符串
     * 
     * @param inputByte
     * @return
     */
    public static String byte1hex(byte[] inputByte) {
        if (null == inputByte) {
            return null;
        }
        String resultStr = "";
        String tmpStr = "";
        for (int n = 0; n < inputByte.length; n++) {
            tmpStr = (Integer.toHexString(inputByte[n] & 0XFF));
            if (tmpStr.length() == 1)
                resultStr = resultStr + "0" + tmpStr;
            else
                resultStr = resultStr + tmpStr;
            if (n < inputByte.length - 1)
                resultStr = resultStr + ":";
        }
        return resultStr.toUpperCase();
    }

    public static String replace(String text, String repl, String with, int max) {
        if (isEmpty(text) || isEmpty(repl) || with == null || max == 0) {
            return text;
        }
        int start = 0;
        int end = text.indexOf(repl, start);
        if (end == -1) {
            return text;
        }
        int replLength = repl.length();
        int increase = with.length() - replLength;
        increase = (increase < 0 ? 0 : increase);
        increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
        StringBuffer buf = new StringBuffer(text.length() + increase);
        while (end != -1) {
            buf.append(text.substring(start, end)).append(with);
            start = end + replLength;
            if (--max == 0) {
                break;
            }
            end = text.indexOf(repl, start);
        }
        buf.append(text.substring(start));
        return buf.toString();
    }
    
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

测试类(MD5Test):

package com.dengsilinming.test;

/**
 *
 * @author dengsilinming
 * @date 2012-12-27 上午10:48:24
 *
 */
public class MD5Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(MD5Utils.encryptByMD5("dengsilinming"));
    }

}

你可能感兴趣的:(MD5加密示例)