参考:
Jasypt是加密库,支持密码、Digest认证、文本、对象加密。
密码加密复合RFC2307标准。http://www.jasypt.org/download.html
spring项目参考:https://blog.csdn.net/gdfsbingfeng/article/details/16886805
仓库地址:https://github.com/ulisesbocchio/jasypt-spring-boot
使用过程也可查看仓库README.md:https://github.com/ulisesbocchio/jasypt-spring-boot
maven官网地址:https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter
配置信息只有 jasypt.encryptor.password
是必须的,配置项有:
需要注意版本对应
jasypt-spring-boot-starter | 依赖的 spring-boot-starter |
---|---|
2.1.0 | 2.0.3.RELEASE 2.2.6.RELEASE |
2.0.0 | 2.0.0.RELEASE 2.2.6.RELEASE |
1.18 | 1.5.10.RELEASE 2.2.6.RELEASE |
1.12 | 1.5.1.RELEASE 2.2.6.RELEASE |
需要注意加解密的类型一致,如:
2.1.2版本默认加密方式为:PBEWithMD5AndDES
3.0.3版本默认加密方式为:PBEWITHHMACSHA512ANDAES_256
当引入3.0.3依赖,却没有添加相关jasypt加解密配置,而密文通过【PBEWithMD5AndDES】来加密,启动会报错。
需要切换为【PBEWITHHMACSHA512ANDAES_256】方式进行。
<dependency>
<groupId>com.github.ulisesbocchiogroupId>
<artifactId>jasypt-spring-boot-starterartifactId>
<version>2.1.2version>
dependency>
<dependency>
<groupId>com.github.ulisesbocchiogroupId>
<artifactId>jasypt-spring-bootartifactId>
<version>2.0.0version>
dependency>
@RestController
public class TestController {
@Value("${test.password:testpassword}")
private String jasyptTestPassword;
@RequestMapping(value = "/testJasypt", method = RequestMethod.GET)
public Object testJasyptPassword(){
return "get password:" + jasyptTestPassword;
}
}
test:
password: ENC(获取到的密文)
# 如果密文加盐,需要配置盐值
jasypt:
encryptor:
password: 盐值
2-1、通过java程序获取密文
// -----默认加解密
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
String passwordEn = stringEncryptor.encrypt("root");
String passwordDe = stringEncryptor.decrypt(passwordEn);
System.out.println("password密文:" + passwordEn);
System.out.println("password明文:" + passwordDe);
}
// -----加盐加解密
@Test
public void test() {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("hello"); //设置加密盐值
String passwordEn = encryptor.encrypt("root");
String passwordDe = encryptor.decrypt(passwordEn);
System.out.println("password密文:" + passwordEn);
System.out.println("password明文:" + passwordDe);
}
// -----加解密工具类
public class JasypUtil {
private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";
private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";
/**
* @Description: Jasyp加密(PBEWithMD5AndDES)
* @Author: Rambo
* @CreateDate: 2020/7/13 10:24
* @UpdateUser: Rambo
* @UpdateDate: 2020/7/13 10:24
* @param plainText 待加密的原文
* @param factor 加密秘钥
* @return java.lang.String
* @Version: 1.0.0
*/
public static String encryptWithMD5(String plainText, String factor) {
// 1. 创建加解密工具实例
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
// 2. 加解密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm(PBEWITHMD5ANDDES);
config.setPassword(factor);
encryptor.setConfig(config);
// 3. 加密
return encryptor.encrypt(plainText);
}
/**
* @Description: Jaspy解密(PBEWithMD5AndDES)
* @Author: Rambo
* @CreateDate: 2020/7/13 10:28
* @UpdateUser: Rambo
* @UpdateDate: 2020/7/13 10:28
* @param encryptedText 待解密密文
* @param factor 解密秘钥
* @return java.lang.String
* @Version: 1.0.0
*/
public static String decryptWithMD5(String encryptedText, String factor) {
// 1. 创建加解密工具实例
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
// 2. 加解密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm(PBEWITHMD5ANDDES);
config.setPassword(factor);
encryptor.setConfig(config);
// 3. 解密
return encryptor.decrypt(encryptedText);
}
/**
* @Description: Jasyp 加密(PBEWITHHMACSHA512ANDAES_256)
* @Author: Rambo
* @CreateDate: 2020/7/25 14:34
* @UpdateUser: Rambo
* @UpdateDate: 2020/7/25 14:34
* @param plainText 待加密的原文
* @param factor 加密秘钥
* @return java.lang.String
* @Version: 1.0.0
*/
public static String encryptWithSHA512(String plainText, String factor) {
// 1. 创建加解密工具实例
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 2. 加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(factor);
config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
config.setKeyObtentionIterations( "1000");
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(plainText);
}
/**
* @Description: Jaspy解密(PBEWITHHMACSHA512ANDAES_256)
* @Author: Rambo
* @CreateDate: 2020/7/25 14:40
* @UpdateUser: Rambo
* @UpdateDate: 2020/7/25 14:40
* @param encryptedText 待解密密文
* @param factor 解密秘钥
* @return java.lang.String
* @Version: 1.0.0
*/
public static String decryptWithSHA512(String encryptedText, String factor) {
// 1. 创建加解密工具实例
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 2. 加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(factor);
config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
config.setKeyObtentionIterations( "1000");
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(encryptedText);
}
}
2-2、通过jasypt中jar包程序获取密文
如个人本地目录:D:\Environmental\RepMaven\org\jasypt\jasypt\1.9.3
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=hello algorithm=PBEWithMD5AndDES
# 输出
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: root
password: hello
----OUTPUT----------------------
aCEx6r9g2lBuGF8w/XU8wQ==
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="aCEx6r9g2lBuGF8w/XU8wQ==" password=hello algorithm=PBEWithMD5AndDES
#输出
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: aCEx6r9g2lBuGF8w/XU8wQ==
password: hello
----OUTPUT----------------------
root
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mytest?characterEncoding=UTF-8&&serverTimezone=GMT
username: root
password: ENC(AxfLzLN0ayIYc7dGypds0g==) # 明文=root
test:
password: ENC(aCEx6r9g2lBuGF8w/XU8wQ==) # 未加盐:bnwAMepYNbDeCkENg+cerQ== 明文=root
# 如果密文加盐,需要配置盐值
jasypt:
encryptor:
password: hello
#访问接口
http://localhost:8080/testJasypt
#结果
get password:root
org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception.
A possible cause is you are using strong encryption algorithms and you have not installed the Java
Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine
# 解决方案:
查看是否是加解密类型不一致导致。
或者下载:https://www.oracle.com/java/technologies/javase-jce8-downloads.html
下载压缩包解压,将local_policy.jar和US_export_policy.jar替换Java\jdk1.8.0_77\jre\lib\security\路径下的jar包
yml中@是特殊字符, 含有@左右需要加单引号。
jasypt 自动加密整个文件的时候,会将单引号也当做密码的一部分,这样得到的密文肯定是错的。
#解决方案:
直接将密码生成,然后再复制过去,不要带双引号。
关于盐值配置方式。注意:关于盐值明文存放到配置文件,一样存在安全风险。
jasypt:
encryptor:
password: hello
jasypt:
encryptor:
password: ${
ENCRYPT:hello} # 系统环境找不到,默认使用hello
在idea启动设置,本地可以正常启动,但是打包到服务器部署需要在idea中Mave打包和部署添加相关参数:
clean package -Djasypt.encryptor.password=hello
java -jar -Djasypt.encryptor.password=123456 springboot-jasypt-test.jar
配置到服务器某文件中,启动时加载文件获取。
不自定义加密类的话,默认算法为 PBEWithMD5AndDES
多次生成,每次生成的密码不一样。不同的密码序列,解密却可以一样。
ENC前缀可改变,即自定义格式:需要添加配置
jasypt:
encryptor:
property:
prefix: "TEST["
suffix: "]"