MD5

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


public class MD5 {
    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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值