Java使用bcrypt實(shí)現(xiàn)對(duì)密碼加密效果詳解
簡(jiǎn)介
本文用示例介紹使用對(duì)密碼進(jìn)行加密的算法:bcrypt。
bcrypt是一種自帶鹽值(自動(dòng)加鹽)的加密方案。
bcrypt加密原理
加密過(guò)程
- 先隨機(jī)生成salt
- salt跟password進(jìn)行hash
注意
- 對(duì)于同一個(gè)密碼,每次生成的hash是不同的
- hash中包含了salt
校驗(yàn)過(guò)程
- 從hash中取出salt
- salt跟password進(jìn)行hash計(jì)算
- 將得到的hash跟數(shù)據(jù)庫(kù)中提取的的hash進(jìn)行比對(duì)返回Boolean類(lèi)型:true/false
bcrypt與md5的區(qū)別
| 項(xiàng) | md5 | bcrypt |
|---|---|---|
| 密文長(zhǎng)度 | 32位 | 60位 |
| 安全性 | 安全性差。 密碼相同時(shí),加密后密文一樣。 提升安全性的方案:加密前生成隨機(jī)的鹽值(字符串),將它與密碼拼接,然后再使用md5加密。 | 安全性好。 密碼相同時(shí),生成的密文是不一樣的。(因?yàn)樗詣?dòng)生成隨機(jī)鹽值) |
| 加密耗時(shí) | 短 | 略長(zhǎng) |
示例
1、引入依賴(lài)
pom.xml加入如下依賴(lài):
<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、寫(xiě)測(cè)試類(lèi)
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è)試
訪(fǎng)問(wèn):http://localhost:8080/test/
多次訪(fǎng)問(wèn)后的后端結(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
$:分割符,無(wú)意義;
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)文章
springboot 集成cas5.3 實(shí)現(xiàn)sso單點(diǎn)登錄詳細(xì)流程
SSO的定義是在多個(gè)應(yīng)用系統(tǒng)中,用戶(hù)只需要登錄一次就可以訪(fǎng)問(wèn)所有相互信任的應(yīng)用系統(tǒng)。單點(diǎn)登錄是目前比較流行的企業(yè)業(yè)務(wù)整合的解決方案之一,本文給大家介紹springboot 集成cas5.3 實(shí)現(xiàn)sso單點(diǎn)登錄功能,感興趣的朋友一起看看吧2021-10-10
RestTemplate的DELETE及PUT等請(qǐng)求方法使用精講
這篇文章主要為大家介紹了RestTemplate的DELETE及PUT等請(qǐng)求方法的使用精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
詳解eclipse中Maven工程使用Tomcat7以上插件的方法
本篇文章主要介紹了詳解eclipse中Maven工程使用Tomcat7以上插件的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
SpringBoot限制接口訪(fǎng)問(wèn)頻率功能實(shí)現(xiàn)
最近在基于SpringBoot做一個(gè)面向普通用戶(hù)的系統(tǒng),為了保證系統(tǒng)的穩(wěn)定性,防止被惡意攻擊,我想控制用戶(hù)訪(fǎng)問(wèn)每個(gè)接口的頻率,接下來(lái)通過(guò)本文給大家介紹SpringBoot限制接口訪(fǎng)問(wèn)頻率功能實(shí)現(xiàn),需要的朋友可以參考下2023-05-05
Spring security實(shí)現(xiàn)對(duì)賬戶(hù)進(jìn)行加密
這篇文章主要介紹了Spring security實(shí)現(xiàn)對(duì)賬戶(hù)進(jìn)行加密,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
命令行使用支持?jǐn)帱c(diǎn)續(xù)傳的java多線(xiàn)程下載器
java命令行下載器,支持?jǐn)帱c(diǎn)續(xù)傳下載,多線(xiàn)程下載,需要的朋友可以參考下2014-02-02
Java正則表達(dá)式之Pattern和Matcher的使用
本文詳細(xì)介紹了Java中處理正則表達(dá)式的Pattern和Matcher類(lèi)的使用方法和實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
springboot中的controller注意事項(xiàng)說(shuō)明
這篇文章主要介紹了springboot中的controller注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

