Java使用bcrypt實(shí)現(xiàn)對(duì)密碼加密效果詳解
簡介
本文用示例介紹使用對(duì)密碼進(jìn)行加密的算法:bcrypt。
bcrypt是一種自帶鹽值(自動(dòng)加鹽)的加密方案。
bcrypt加密原理
加密過程
- 先隨機(jī)生成salt
- salt跟password進(jìn)行hash
注意
- 對(duì)于同一個(gè)密碼,每次生成的hash是不同的
- hash中包含了salt
校驗(yàn)過程
- 從hash中取出salt
- salt跟password進(jìn)行hash計(jì)算
- 將得到的hash跟數(shù)據(jù)庫中提取的的hash進(jìn)行比對(duì)返回Boolean類型:true/false
bcrypt與md5的區(qū)別
項(xiàng) | md5 | bcrypt |
---|---|---|
密文長度 | 32位 | 60位 |
安全性 | 安全性差。 密碼相同時(shí),加密后密文一樣。 提升安全性的方案:加密前生成隨機(jī)的鹽值(字符串),將它與密碼拼接,然后再使用md5加密。 | 安全性好。 密碼相同時(shí),生成的密文是不一樣的。(因?yàn)樗詣?dòng)生成隨機(jī)鹽值) |
加密耗時(shí) | 短 | 略長 |
示例
1、引入依賴
pom.xml加入如下依賴:
<dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.4</version> </dependency>
總的pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo_SpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo_SpringBoot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.0.RELEASE</version> </plugin> </plugins> </build> </project>
2、寫測(cè)試類
package com.example.controller; import org.mindrot.jbcrypt.BCrypt; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/test") public String test() { String password = "123456"; // 加密 String encodedPassword = BCrypt.hashpw(password, BCrypt.gensalt()); System.out.println(encodedPassword); // 使用正確密碼驗(yàn)證密碼是否正確 boolean flag = BCrypt.checkpw(password, encodedPassword); System.out.println(flag); // 使用錯(cuò)誤密碼驗(yàn)證密碼是否正確 flag = BCrypt.checkpw("111222", encodedPassword); System.out.println(flag); System.out.println("-------------------------------------------"); return "test success"; } }
3、測(cè)試
訪問:http://localhost:8080/test/
多次訪問后的后端結(jié)果:
$2a$10$63I66GOCxncIufBHEzcbF.LUBA45jCFwATVXz7MTzp7bpDn.SQMSG
true
false
-------------------------------------------
$2a$10$CV7iT/TpZVx23IdEvMHhleRSnIPPI2N/s..Cl9Bd50V2LFdff1woa
true
false
-------------------------------------------
$2a$10$wNTnhUedcx0InkAflqWm0O9M163WRR/RCGLdBSfhrgzJQuBZoEeEG
true
false
-------------------------------------------
密文含義
示例密文:
$2a$10$CV7iT/TpZVx23IdEvMHhleRSnIPPI2N/s..Cl9Bd50V2LFdff1woa
$:分割符,無意義;
2a:bcrypt加密版本號(hào);
10:cost的值(默認(rèn)值);
之后的22位:salt值;
之后:密碼的密文
到此這篇關(guān)于Java使用bcrypt實(shí)現(xiàn)對(duì)密碼加密效果詳解的文章就介紹到這了,更多相關(guān)Java bcrypt密碼加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用POI實(shí)現(xiàn)導(dǎo)出Excel的方法詳解
在項(xiàng)目開發(fā)中往往需要使用到Excel的導(dǎo)入和導(dǎo)出,導(dǎo)入就是從Excel中導(dǎo)入到DB中,而導(dǎo)出就是從DB中查詢數(shù)據(jù)然后使用POI寫到Excel上。本文將利用POI實(shí)現(xiàn)導(dǎo)出Excel,需要的可以參考一下2022-10-10動(dòng)態(tài)配置Spring Boot日志級(jí)別的全步驟
這篇文章主要給大家介紹了關(guān)于動(dòng)態(tài)配置Spring Boot日志級(jí)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng)
基礎(chǔ)扎不扎實(shí)只有在實(shí)戰(zhàn)中才能顯現(xiàn),本篇文章手把手帶你用Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01SpringBoot中使用Redisson的實(shí)現(xiàn)示例
Redission是一個(gè)強(qiáng)大的Java庫,用于構(gòu)建和管理分布式系統(tǒng)中的緩存和任務(wù)調(diào)度,本文主要介紹了SpringBoot中使用Redisson的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-12-12SpringBoot中的五種對(duì)靜態(tài)資源的映射規(guī)則的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot中的五種對(duì)靜態(tài)資源的映射規(guī)則的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12使用Runnable實(shí)現(xiàn)數(shù)據(jù)共享
這篇文章主要為大家詳細(xì)介紹了如何使用Runnable實(shí)現(xiàn)數(shù)據(jù)共享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07