The project is a maven project, first add the following dependencies to pom.xml.
<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>
Write the tool class MD5Utils.java
The algorithm tool class DigestUtils is mainly used , which is located in the package directory org.apache.commons.codec.digest.
public class MD5Utils { private static final String salt = "1h2s3w"; private static String MD5 (String src) { return DigestUtils.md5Hex(src); } /* * Enter password to form password * @author hsw * @date 2018/4/24 21:22 * @param [inputPass] * @return java.lang.String */ public static String inputPassToFormPass(String inputPass) { return MD5(salt.charAt(0) + salt.charAt(4) + salt.charAt(3) + inputPass + salt.charAt(2) + salt.charAt(1) + salt.charAt(5)); } /* * Convert form password to stored password * @author hsw * @date 2018/4/24 21:22 * @param [formPass, salt] * @return java.lang.String */ public static String formPassToDBPass(String formPass, String salt) { return MD5(salt.charAt(0) + salt.charAt(4) + salt.charAt(3) + formPass + salt.charAt(2) + salt.charAt(1) + salt.charAt(5)); } public static String inputPassToDbPass (String inputPass, String salt) { String formPass = inputPassToFormPass(inputPass); String DBPass = formPassToDBPass(formPass,salt); return DBPass; } public static void main(String[] args) { String inputPass = "hsw666666"; String salt = "2311231"; String res = inputPassToDbPass(inputPass,salt); System.out.println(res); } }
In the tool class, md5 encryption is performed on the entered password twice. The first time the input password is converted to a form password with a fixed salt, the second time the form password is converted to a stored password with a custom salt, and the input password is encrypted twice.
The main method test result is: 7e6b9947fdda3d5a777972bdd49865f3