<dependency>
<groupId>com.github.ulisesbocchiogroupId>
<artifactId>jasypt-spring-boot-starterartifactId>
<version>3.0.4version>
dependency>
.m2\repository\org\jasypt\jasypt\1.9.3
比如用户名和密码分别是 root 和 123zxcv
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=haha algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
E:\>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123zxcv" password=haha algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.201-b09
----ARGUMENTS-------------------
ivGeneratorClassName: org.jasypt.iv.RandomIvGenerator
algorithm: PBEWITHHMACSHA512ANDAES_256
input: 123zxcv
password: haha
keyObtentionIterations: 10000
----OUTPUT----------------------
/t8E+snZ4ODAwQU9gj0eA6MRnL98PHEGJVtOYN44wzmNKqHrY0TciomqfQg2qb2w
OUTPUT输出参数则为密文
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=haha algorithm=PBEWithMD5AndDES ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
E:\>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=haha algorithm=PBEWithMD5AndDES ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.201-b09
----ARGUMENTS-------------------
ivGeneratorClassName: org.jasypt.iv.RandomIvGenerator
algorithm: PBEWithMD5AndDES
input: root
password: haha
keyObtentionIterations: 10000
----OUTPUT----------------------
gf0gosWSje2GwnF6KWKHbaUDeI1PLo1K
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="/t8E+snZ4ODAwQU9gj0eA6MRnL98PHEGJVtOYN44wzmNKqHrY0TciomqfQg2qb2w" password=haha algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
E:\>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="/t8E+snZ4ODAwQU9gj0eA6MRnL98PHEGJVtOYN44wzmNKqHrY0TciomqfQg2qb2w" password=haha algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.201-b09
----ARGUMENTS-------------------
ivGeneratorClassName: org.jasypt.iv.RandomIvGenerator
algorithm: PBEWITHHMACSHA512ANDAES_256
input: /t8E+snZ4ODAwQU9gj0eA6MRnL98PHEGJVtOYN44wzmNKqHrY0TciomqfQg2qb2w
password: haha
keyObtentionIterations: 10000
----OUTPUT----------------------
123zxcv
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="gf0gosWSje2GwnF6KWKHbaUDeI1PLo1K" password=haha algorithm=PBEWithMD5AndDES ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
E:\>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="gf0gosWSje2GwnF6KWKHbaUDeI1PLo1K" password=haha algorithm=PBEWithMD5AndDES ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=10000
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.201-b09
----ARGUMENTS-------------------
ivGeneratorClassName: org.jasypt.iv.RandomIvGenerator
algorithm: PBEWithMD5AndDES
input: gf0gosWSje2GwnF6KWKHbaUDeI1PLo1K
password: haha
keyObtentionIterations: 10000
----OUTPUT----------------------
root
package com.ashen.heihei.util;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
/**
* Jasypt加解密工具类
* @author HY
*/
public class JasyptUtil {
private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";
private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";
public static void main(String[] args) {
String secret1 = encryptWithMD5("root", "haha");
System.out.println(secret1);
String name1 = decryptWithMD5(secret1, "haha");
System.out.println(name1);
String secret2 = encryptWithMD5("123zxcv", "haha");
System.out.println(secret2);
String name2 = decryptWithMD5(secret2, "haha");
System.out.println(name2);
}
/**
* jasypt 加密(PBEWithMD5AndDES)
* @param message 待加密的原文
* @param password 加解密秘钥
* @return
*/
public static String encryptWithMD5(String message, String password) {
// 1. 创建加解密工具实例
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
// 2. 加解密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm(PBEWITHMD5ANDDES);
config.setPassword(password);
// 设置迭代次数
config.setKeyObtentionIterations( "10000");
// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
// 3. 加密
return encryptor.encrypt(message);
}
/**
* jasypt 解密(PBEWithMD5AndDES)
* @param encryptedMessage 待解密的原文
* @param password 加解密秘钥
* @return
*/
public static String decryptWithMD5(String encryptedMessage, String password) {
// 1. 创建加解密工具实例
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
// 2. 加解密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm(PBEWITHMD5ANDDES);
config.setPassword(password);
// 设置迭代次数
config.setKeyObtentionIterations( "10000");
// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
// 3. 解密
return encryptor.decrypt(encryptedMessage);
}
/**
* jasypt 加密 (PBEWITHHMACSHA512ANDAES_256)
* @param message 待加密的原文
* @param password 加解密秘钥
* @return
*/
public static String encryptWithSHA512(String message, String password) {
// 1. 创建加解密工具实例
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 2. 加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 设置迭代次数
config.setKeyObtentionIterations( "10000");
// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
// 3. 加密
return encryptor.encrypt(message);
}
/**
* jasypt 解密(PBEWITHHMACSHA512ANDAES_256)
* @param encryptedMessage 待解密的原文
* @param password 加解密秘钥
* @return
*/
public static String decryptWithSHA512(String encryptedMessage, String password) {
// 1. 创建加解密工具实例
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 2. 加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 设置迭代次数
config.setKeyObtentionIterations( "10000");
// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
// 3. 解密
return encryptor.decrypt(encryptedMessage);
}
}
几个重要的参数:
encryptor:
password: haha
algorithm: PBEWITHHMACSHA512ANDAES_256
key-obtention-iterations: 10000
spring:
datasource:
url: jdbc:mysql://localhost:3306/heheda?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
# username: root
# password: 123zxcv
username: ENC(ANCT18nxVcrbxXc2niY7JBKK+4ORvGCgvEUBr4wobWn/QZjNlKLsYbuhnyHGx4lW)
password: ENC(tHJC+FMYPe12iwvIj+nJkc5Vks+fPAJ9sYe2utWabbYtToxnYwjsvWjPrid8hY6M)
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
dbcp2:
max-wait-millis: 10000
min-idle: 5
initial-size: 5
max-total: 100
配置后,直接启动 springboot 即可
java -jar jasypt-demo.jar --jasypt.encryptor.password=haha
java -Djasypt.encryptor.password=haha -jar jasypt-demo.jar
export JASYPT_ENCRYPTOR_PASSWORD=haha
jasypt:
encryptor:
password: ${JASYPT_ENCRYPTOR_PASSWORD}
algorithm: PBEWITHHMACSHA512ANDAES_256
key-obtention-iterations: 10000
package com.ashen.heihei.config;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* jasypt 加解密配置
* @Author: HY
*/
@Configuration
public class JasyptConfig {
/**
* 加解密秘钥
*/
private static String PASSWORD = "haha";
/**
* 加密算法
*/
private static String ALGORITHM = "PBEWITHHMACSHA512ANDAES_256";
/**
* 迭代次数 iterations
*/
private static String ITERATIONS = "10000";
@Bean("jasyptStringEncryptor")
public PooledPBEStringEncryptor jasyptEncryptor(){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(PASSWORD);
config.setAlgorithm(ALGORITHM);
config.setKeyObtentionIterations(ITERATIONS);
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
return encryptor;
}
}
https://www.oracle.com/java/technologies/javase-jce8-downloads.html