Code implementation of custom MD5 salt encryption method

   Salt and encrypt the password according to your own understanding. When the user registers, the salt value will be generated first, saved, and then the account and password will be saved. When a user logs in, we need to first query our salt value through our username, and then match the corresponding database according to the salt value and password. Of course, there may be a problem that a user name has multiple salt values, which may also be the unique identification of many website registrations using the user name. Of course, it is also related to the demand, and the user name cannot be modified. If there are more than one, it needs to be compared in a loop. This encryption mainly focuses on rewriting MD5 and adding a salt value to save it, which is double secrecy, which should prevent brute force cracking.
Let's see the code:
package demo.dcn.service.utils.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {
	// global array
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f","h","g","i","j","k","m","n","o",
            "p","q","x","y","z","u","w","=","+","-","^","*","#","v"};

    public Md5Util() {
    	
    }
    	/**
    	 * Returns in the form of numbers and strings
    	 * @param bByte
    	 * @return
    	 */
	    private static String byteToArrayString(byte bByte) {
	        int iRet = bByte;
	        // System.out.println("iRet="+iRet);
	        if (iRet < 0) {
	            iRet += 256;
	        }
	        int iD1 = iRet / 38;
	        int iD2 = iRet % 38;
	        return strDigits[iD1] + strDigits[iD2];
	    }
	    /**
	     * Convert byte array to hexadecimal string
	     * @param bByte
	     * @return
	     */
	    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();
	    }
	    /**
	     * HASH encryption
	     * @param strObj
	     * @return
	     */
	    public static String GetMD5Code(String strObj) {
	        String resultString = null;
	        try {
	            resultString = new String(strObj);
	            MessageDigest md = MessageDigest.getInstance("MD5");
	            // md.digest() The return value of this function is a byte array that stores the result of the hash value
	            resultString = byteToString(md.digest(strObj.getBytes()));
	        } catch (NoSuchAlgorithmException ex) {
	            ex.printStackTrace();
	        }
	        return resultString;
	    }
	    public static void main(String[] args) {
	    	Md5Util getMD5 = new Md5Util();
	        System.out.println(getMD5.GetMD5Code("0123"));
	        System.out.println(getMD5.GetMD5Code("0123"));
	    }
}


package demo.dcn.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import demo.dcn.dao.LookerDaoMapper;
import demo.dcn.service.RegisterService;
import demo.dcn.service.utils.security.Md5Util;
import demo.dcn.type.ResultMap;
import demo.dcn.vo.Looker;
import demo.dcn.vo.LookerSalt;
@Service
public class RegisterServiceImpl implements RegisterService {
	
	@Resource
	private LookerDaoMapper lookerDaoMapper;

	@Override
	public ResultMap lookerRegister(Looker looker) {
		ResultMap resultMap = ResultMap.SUCCESS;
		lookerDaoMapper.lookerRegisterDao(looker);
		return resultMap;
	}

	@Override
	public void lookerSaltRegister(LookerSalt lookerSalt) {
		lookerDaoMapper.lookerSaltReDao(lookerSalt);
	}
	
	@Override
	public Looker lookerLogin(Looker looker) {
		List<String> salts = lookerDaoMapper.find(looker.getLookerName());//May query multiple salt values
		Looker	looker2 = null;
		if(salts!=null&&salts.size()>0){
			String password = looker.getLookerPassword();
			for (String salt : salts) {
				looker.setLookerPassword(Md5Util.GetMD5Code(password)+salt);
				looker2 = lookerDaoMapper.lookerLogin(looker);
				if(looker2!=null){//Return if the corresponding account is matched
					break;
				}
			}
		}
		return looker2;
	}
}


import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import demo.dcn.service.RegisterService;
import demo.dcn.service.utils.UuidUtils;
import demo.dcn.service.utils.security.Md5Util;
import demo.dcn.vo.Looker;
import demo.dcn.vo.LookerSalt;
/**
 * test
 * @author [email protected]
 *
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
		"classpath*:config/demo-spring-context.xml",
		"classpath*:config/demo-spring-datasource.xml",
		"classpath*:config/demo-spring-redis.xml",
		"classpath*:config/demo-sql-config.xml"
		
})
public class test {
	
	@Autowired
	private RegisterService registerService;
	@Test
	public void testRegist(){
		LookerSalt lookerSalt = new LookerSalt();
		String uuid = UuidUtils.getuuid();
		lookerSalt.setLookerName("张三");
		lookerSalt.setUuid(uuid);
		registerService.lookerSaltRegister(lookerSalt);
		Looker looker = new Looker();
		looker.setLookerName("张三");
		looker.setLookerLevel(1);
		looker.setLoginStatus(0);
		looker.setLookerPassword(Md5Util.GetMD5Code("adcv0123fsac")+uuid);//加密
		registerService.lookerRegister(looker);
		System.out.println("a");
	}

	@Test
	public void testLogin(){
		Looker looker = new Looker();
		looker.setLookerName("张三");
		looker.setLookerPassword("adcv0123fsac");
		Looker looker2=	registerService.lookerLogin(looker);
		if(looker2!=null){
			System.out.println("Login successful");
			looker2.toString();
		}else{
			System.out.println("Login failed");
		}
	}


The main logic business code is written here, and other codes are not written. The salt value is used by uuid. Of course, you can also define it yourself and use other salt values.
Password saved in database: 3y166d4b4#4=4w2x3j5p2u1n602#2e4747c4aceee805427696846f3a83f880be
Salt value of database: 47c4aceee805427696846f3a83f880be




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676832&siteId=291194637