MD5加密和Bcrypt加密

本文介绍并对比了MD5和Bcrypt两种密码加密方式,详细展示了如何使用Spring框架的DigestUtils进行MD5加密,以及如何利用jBCrypt库进行更安全的Bcrypt加密。通过实际代码演示了加密过程,并提供了Bcrypt的jar包下载链接及Maven配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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
<!-- https://mvnrepository.com/artifact/org.mindrot/jbcrypt -->
<dependency>
    <groupId>org.mindrot</groupId>
    <artifactId>jbcrypt</artifactId>
    <version>0.4</version>
</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);
	}

}

实验效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值