jbcrypt对明文密码加密以及解密的使用

一.添加maven依赖

<dependency>
    <groupId>org.mindrot</groupId>
    <artifactId>jbcrypt</artifactId>
    <version>0.4</version>
</dependency>

二.代码部分

import org.mindrot.jbcrypt.BCrypt;
public class BcryptUtil {
    public static String encode(String password){
        return BCrypt.hashpw(password, BCrypt.gensalt());             //对明文密码进行加密,并返回加密后的密码
    }

    public static boolean match(String password, String encodePassword){          //将明文密码跟加密后的密码进行匹配,如果一致返回true,否则返回false
        return BCrypt.checkpw(password,encodePassword);
    }

    public static void main(String[] args){
        String password = "123456";     //明文密码
        String psd = BcryptUtil.encode(password);       //加密后的密码
        System.out.println(BcryptUtil.match(password, psd));            //如果一致,返回true,否则返回false
    }
}

三.测试结果

image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容