其他分享
首页 > 其他分享> > MD5加密解密以及设置salt(盐值)

MD5加密解密以及设置salt(盐值)

作者:互联网

MD5算法

package com.oracle.jsp.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * 
 * @author wjxing
 *
 */
public class MD5 {
    
    // 全局数组
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    public MD5() {
    }

    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    // 返回形式只为数字
    @SuppressWarnings("unused")
    private static String byteToNum(byte bByte) {
        int iRet = bByte;
        System.out.println("iRet1=" + iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        return String.valueOf(iRet);
    }

    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }

    public static String GetMD5Code(String strObj) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            // md.digest() 该函数返回值为存放哈希值结果的byte数组
            resultString = byteToString(md.digest(strObj.getBytes()));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }

}

设置盐值(salt)的算法

package com.oracle.jsp.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StringUtil {

    /**
     * String转Int
     * @param str
     * @return
     */
    public static int StringToInt(String str) {
        int result = 0;
        try {
            result = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            result = 0;
            //e.printStackTrace();
        }
        return result;
    }
    /**
     * 获取盐值(随机字符串)
     * @param length
     * @return
     */
    public static String getRandomString(int length) { 
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";   
        Random random = new Random();   
        StringBuffer sb = new StringBuffer();   
        for (int i = 0; i < length; i++) {   
            int number = random.nextInt(base.length());   
            sb.append(base.charAt(number));   
        }   
        return sb.toString();   
     }
    
    public static List<String> splitProperties (String properties){
        List<String> options = new ArrayList<String>();
        String option = null;
        String[] strings = properties.split(",");
        for (String string : strings) {
            if(!"0".equals(string)){
                option = string.charAt(3)+"";
                options.add(option);
                
            }
        }
        return options;
        
    }
    /**
     * String转float
     * @param str
     * @return
     */
    public static float strToFlo(String str) {
        float i = 0;
        try {
            i = Integer.parseInt(str);
        } catch (Exception e) {
        }
        return i;
    }
}

具体的使用方法

加密
String salt = StringUtil.getRandomString(10);//获取指定长度的盐值
String md5Pwd = 5.GetMD5Code(MD5.GetMD5Code(password)+salt);//调用加密方法对输入的密码以及盐值进行加密,随后需要将密文以及盐值存入到数据库中

解密

rs.getString("password").equals(MD5.GetMD5Code(password+rs.getString("salt")))   //对登录时输入的密码再加上数据库中对应的salt重新进行加密,判断加密后的密文与数据库中存储的是否相同

 

标签:return,String,int,iRet,盐值,static,salt,public,MD5
来源: https://www.cnblogs.com/1gaoyu/p/11729255.html