<!-- 加密工具 -->java
<dependency>
mysql
<groupId>com.github.ulisesbocchio</groupId>
git
<artifactId>jasypt-spring-boot-starter</artifactId>
github
<version>1.8</version>
spring
</dependency>
sql
配置加密參數(能夠理解爲加密的salt)數據庫
jasypt:maven
encryptor:spring-boot
password: 123456(這個能夠隨便設置的,每次設置時數據庫的接連驅動要是明文(也就是不加密的,不然運行起來報錯))工具
@Autowired StringEncryptor stringEncryptor;
@Test public void encryptPwd() {
//調用加密方法
String result = stringEncryptor.encrypt("yourpassword"); System.out.println("==================");
System.out.println(result);
//調用解密方法
String test=stringEncryptor.decrypt(result)
System.out.println(test);
System.out.println("==================");
}
把獲得的密文寫到須要使用到的地方,加密後的字符串須要放到ENC裏面,格式以下:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false
username: root
password: ENC(4TyrSSgQd2DCHnXVwkdKMQ==)
driver-class-name: com.mysql.jdbc.Driver
經過命令行運行 jasypt-1.9.2.jar 包命令來加密解密:
在jar包所在目錄打開命令行,運行以下加密命令:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=security algorithm=PBEWithMD5AndDES
這裏input輸入的能夠對數據庫的用戶名進行加密也能夠是數據庫的密碼進行加密
運行結果以下(這種加密每次獲得的密文都是不同的)
使用剛纔加密出來的結果進行解密,執行以下解密命令:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="i00VogiiZ1FpZR9McY7XNw==" password=security algorithm=PBEWithMD5AndDES
結果以下