Java中MD5简单加密处理。

简单的处理一下加密。

package com.yj.until;

import java.security.MessageDigest;

public class MD5Code {

    private final static String[] strDigihs = { "0", "1", "2", "3", "4", "5","6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    /**
     * @Description:对字符串进行md5加密
     * @param code 待加密内容
     * @return 加密后密文
     * @author yangjing  
     * @date 2017-2-18 下午2:22:30
     */
    public static String getMD5Code(String code){
        String result=""; 
        try {
            MessageDigest md=MessageDigest.getInstance("MD5");
            result=byteToString(md.digest(code.getBytes("utf-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    } 

     /**
     * @Description: 转换字节数组变成16进制字串
     * @return String    返回类型  
     * @author yangjing  
     * @date 2017-2-18 下午2:22:30
      */
    public static String byteToString(byte[] byts){
        StringBuffer sb=new StringBuffer();
        for (byte byt : byts) {
            sb.append(byteToArrayString(byt));
        }
        return sb.toString();
    }

    /**
     * @Description:返回形式是数字或者字符
     * @return String    返回类型  
     * @author yangjing  
     * @date 2017-2-18 下午2:22:30
      */
    public static String byteToArrayString(byte byt){
        int irl= byt;
        if(irl<0){
            irl+=256;
        }
        int d1=irl/16;
        int d2=irl%16;
        return strDigihs[d1]+strDigihs[d2];
    }

    /**
     * @Description:验证MD5密码是否正确
     * @param newCode  未加密的字符串
     * @param oldCode  加密的字符串
     * @return  true 一样 ,false 不同
     * @author yangjing  
     * @date 2017-2-18 下午2:22:30
     */
    public static boolean cheackMD5Code(String newCode,String oldCode){
        boolean result=false;
        if(getMD5Code(newCode).equals(oldCode)){
            result=true;
        }
        return result;
    }

}

猜你喜欢

转载自blog.csdn.net/jingyang07/article/details/55667143