MD5 encryption, simple implementation of springboot

	<dependency>
	    <groupId>commons-codec</groupId>
	    <artifactId>commons-codec</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-lang3</artifactId>
	    <version>3.6</version>
	</dependency>

After adding dependencies, directly use:

import org.apache.commons.codec.digest.DigestUtils;
DigestUtils.md5Hex()

Insert picture description here
Use this method for md5 encryption.
This is my tool class;

package com.imooc.miaosha.util;

import org.apache.commons.codec.digest.DigestUtils;

public class MD5Util {
	
	public static String md5(String src) {
		return DigestUtils.md5Hex(src);
	}
	
	private static final String salt = "1a2b3c4d";
	
	public static String inputPassToFormPass(String inputPass) {
		String str = ""+salt.charAt(0)+salt.charAt(2) + inputPass +salt.charAt(5) + salt.charAt(4);
		System.out.println(str);
		return md5(str);
	}
	
	public static String formPassToDBPass(String formPass, String salt) {
		String str = ""+salt.charAt(0)+salt.charAt(2) + formPass +salt.charAt(5) + salt.charAt(4);
		return md5(str);
	}
	
	public static String inputPassToDbPass(String inputPass, String saltDB) {
		String formPass = inputPassToFormPass(inputPass);
		String dbPass = formPassToDBPass(formPass, saltDB);
		return dbPass;
	}
	
	public static void main(String[] args) {
		System.out.println(inputPassToFormPass("123456"));//d3b1294a61a07da9b49b6e22b2cbd7f9
//		System.out.println(formPassToDBPass(inputPassToFormPass("123456"), "1a2b3c4d"));
//		System.out.println(inputPassToDbPass("123456", "1a2b3c4d"));//b7797cce01b4b131b433b6acf4add449
	}
	
}

use:

The general encryption process is: the
back end
engages in two encryption methods: 1. Plain text + fixed salt => md5.1
2. salt => md5.2 in md5.1 + db
md5.2 is the value stored in the DB
. . . . . . . . . . . . . . . . . . . . . . . . . .
Registration:
Directly call method 1, 2 and encrypt twice, save in db
login:
1. The front end uses md5 encryption with js and then passes it to the back end (the encryption method is the same as the back end method 1, the addition of salt and salt are both It must be the same)
2. The backend is equal to md5.1, and then take out the salt encryption in the user db to form md5.2, and compare with the value in db to see if they are equal

Summary: The user password cannot be seen in db, and it is also encrypted to compare whether the incoming password is equal to that in db. This prevents db from being seen by others.
Also, the password from the front end must also be encrypted before being transmitted to the back end to prevent being caught and seeing the password.

Published 56 original articles · Like1 · Visits1509

Guess you like

Origin blog.csdn.net/weixin_44841849/article/details/105277478