1、添加依赖
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
2.加密算法实现
package com.everjiankang.miaosha.util; import org.apache.commons.codec.digest.DigestUtils; /** * 2次MD5加密 * @author guchunchao * */ public class MD5Util { private static final String PUBLIC_SALT = "1a2b3c4d5e"; /** step1 将input框输入的明文密码根据公有盐转化为MD5加密密码*/ public static String inputPassToFormPass(String password) { String saltPass = PUBLIC_SALT.charAt(0) + PUBLIC_SALT.charAt(2) + password + PUBLIC_SALT.charAt(5) + PUBLIC_SALT.charAt(4); return DigestUtils.md5Hex(saltPass); } /** step2 将根据公有盐转化的MD5加密密码 再根据私有盐进行第二次加密,同时应该在DB中存储私有盐*/ public static String formPassToDBPass(String formPass,String privateSalt) { String saltPass = privateSalt.charAt(0) + privateSalt.charAt(2) + formPass + privateSalt.charAt(5) + privateSalt.charAt(4); return DigestUtils.md5Hex(saltPass); } /** 将step1 和 step2 结合起来进行封装*/ public static String inputPassToDBPass(String inputPass,String privateSalt) { return formPassToDBPass(inputPassToFormPass(inputPass),privateSalt); } public static void main(String[] args) { System.out.println(inputPassToFormPass("123456")); System.out.println(formPassToDBPass("123456","xiaochaoaidami")); System.out.println(inputPassToDBPass("123456","xiaochaoaidami")); } }