MD5加密自定义

98 阅读1分钟
public class MD5Utils {

    /**
     * 加密
     *
     * @param context
     */
    public static String encrypByMd5(String context) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(context.getBytes());//update处理  
            byte[] encryContext = md.digest();//调用该方法完成计算

            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < encryContext.length; offset++) {//做相应的转化(十六进制)  
                i = encryContext[offset];
                if (i < 0) i += 256;
                if (i < 16) buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(encrypByMd5("123456"));
        //e10adc3949ba59abbe56e057f20f883e
        // 加盐,迭代
        System.out.println(encrypByMd5("haha" + "123456"));
        //迭代+把加密的结果再加密
        String str = encrypByMd5("haha" + "123456");
        for (int i = 0; i < 5; i++) {
            str = encrypByMd5(str);
        }
        System.out.println(str);
    }
}