java专题Java 杂谈

分享一个MD5加密工具

2018-01-04  本文已影响23人  H_Man
import java.security.MessageDigest;

public class RdEncrypt {

    public final static String MD5(String sign) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] strTemp = sign.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static String MD5To16(String sign) {
        String str = RdEncrypt.MD5(sign);
        if (StringUtil.isBlank(str)) {
            return null;
        }
        return str.substring(8, 24).toString();
    }

    public static String MD5To16Fission(String sign) {
        String str = RdEncrypt.MD5("Fission" + sign);
        if (StringUtil.isBlank(str)) {
            return null;
        }
        return str.substring(8, 24).toString();
    }
}



上一篇下一篇

猜你喜欢

热点阅读