SpringBoot 集成 Jasypt 对数据库密码加密以及踩坑
参考
文档1: https://blog.csdn.net/qq_37143673/article/details/107563064
文档2: https://zhuanlan.zhihu.com/p/518439781
实测好用, 感觉原作者。
核心: 加密方法
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; public class JasyptUtil { /** * Jasypt生成加密结果 * @param password 配置文件中设定的加密盐值 * @param value 加密值 * @return */ public static String encyptPwd(String password,String value){ PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); encryptor.setConfig(cryptor(password)); String result = encryptor.encrypt(value); return result; } /** * 解密 * @param password 配置文件中设定的加密盐值 * @param value 解密密文 * @return */ public static String decyptPwd(String password,String value){ PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); encryptor.setConfig(cryptor(password)); String result = encryptor.decrypt(value); return result; } 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.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setStringOutputType("base64"); return config; } public static void main(String[] args) { // 加密 String encPwd = encyptPwd("jasypt", "123456"); // 解密 String decPwd = decyptPwd("jasypt", encPwd); System.out.println(encPwd); System.out.println(decPwd); } }