MD5加密和Bcrypt加密

一、MD5加密

//引入包
import org.springframework.util.DigestUtils;

@Controller
@RequestMapping("/resetpwd")
public class ResetpwdController {
	@RequestMapping("/resetpassword")
	@ResponseBody
	public int resetpassword(Member member, String username, String pwd) {
		//加密
		pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
		member.setPassword(pwd);
		int i = memberService.resetPasswordByUsername(member, username);
		return i;
	}
}

二、Bcrypt加密

由于现在彩虹表的出现,使得即使不可逆的MD5加密也变得伪可逆(大数据存储,然后查询),所以采用更为安全的Bcrypt加密。

jar包下载

百度云 https://pan.baidu.com/s/1RU2ptA6uq4LrRuFqUhZ5bw
maven存储库 https://mvnrepository.com/artifact/org.mindrot/jbcrypt

pom.xml


<dependency>
    <groupId>org.mindrotgroupId>
    <artifactId>jbcryptartifactId>
    <version>0.4version>
dependency>

package test;

import org.mindrot.jbcrypt.BCrypt;

public class Test1 {

	public static void main(String[] args) {
		String password = "123456";//$2a$10$GRS6nKF9Fp6ie7Vkj4KHI.LBQZDhzgQ2OcIu8sI90ucaL/wJ0D7He
		//加密
		String pwt = BCrypt.hashpw(password, BCrypt.gensalt());
		//解密
		boolean pswFlag = BCrypt.checkpw(password,"$2a$10$GRS6nKF9Fp6ie7Vkj4KHI.LBQZDhzgQ2OcIu8sI90ucaL/wJ0D7He");

		System.out.println(pwt+"===="+pswFlag);
	}

}

实验效果:
MD5加密和Bcrypt加密_第1张图片

你可能感兴趣的:(MD5加密和Bcrypt加密)