jasypt加密解密
1. 导入依赖
<!--jasypt--> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
2. yml配置
导入依赖后,即可进行jasypt的加密配置
# jasypt 配置加密 jasypt: encryptor: # 密码盐值(
自定义) password: jasypt # 设置加密算法 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.RandomIvGenerator salt-generator-classname: org.jasypt.salt.RandomSaltGenerator
上述操作完成后,可在需要加密的地方配置。
例如,使用格式配置数据库的帐户密码ENC(加密后的密码)
进行加密。
username: ENC(vyxdS47pJdWBF38TFdmjKmMm4zEO0FQP) password: ENC(vyxdS47pJdWBF38TFdmjKmMm4zEO0FQP)
但是如何获得这些加密值呢?接下来,写一个加密解密工具
3. 加密解密工具
package com.example.util; import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; public class JasyptUtils {
/** * 加密 * @param password 配置文件中设置的加密盐值 * @param value 加密值 * @return 加密字符串 */ public static String encrypt(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(cryptor(password));
return encryptor.encrypt(value);
}
/** * 解密 * @param password 配置文件中设定的加密盐值 * @param value 解密密文 * @return 解密后的字符串 */
public static String decrypt(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(cryptor(password));
return encryptor.decrypt(value);
}
/** * 配置,对应yml中的配置 * @param password 盐值 * @return SimpleStringPBEConfig */
public static SimpleStringPBEConfig cryptor(String password){
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
//设置盐值
config.setPassword(password);
//设置算法
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
return config;
}
public static void main(String[] args) {
// 加密
String encryptStr = encrypt("jasypt", "root");
// 解密
String decryptStr = decrypt("jasypt", encryptStr);
System.out.println("加密后:" + encryptStr);
System.out.println("解密后:" + decryptStr);
}
}
在main方法中执行可以得到加密前后的字符串
加密后:b1tEArsy2JkB3h29nD5qs9Kx1qdXYmK8
解密后:root
4. 测试一下
application.yml新加一个配置
hello:
port: ENC(wqy7gE7IBhupjgdPI/FuOTwOQ9p1NAIf)
新建一个TestConfig.java来获取yml中的值
package com.example.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
@Data
public class TestConfig {
@Value("${hello.port}")
private String port;
}
创建测试类测试:
package com.example.jasypt;
import com.example.config.TestConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
public class JasyptTests {
@Resource
private TestConfig testConfig;
@Test
void getPort(){
System.out.println("testConfig.getPort() = " + testConfig.getPort());
}
}
启动后输出:testConfig.getPort() = root
5.隐藏设置的盐值
那么问题来了,盐值肯定是不能被别人知道的。 去掉配置文件中设置的盐值,并在idea的启动配置项的“VM options”添加-Djasypt.encryptor.password=盐值
。 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-780l4p4S-1656068504206)(E:\要整理的笔记\new\image-20210508111230392.png)]
打包后启动方式:java -jar -Djasypt.encryptor.password=盐值 test.jar