Jasypt报错全面解析
Jasypt(Java Simplified Encryption)是一个用于加密和解密数据库敏感信息的工具,在Spring Boot应用中广泛使用,在使用过程中,开发者可能会遇到各种报错问题,本文将详细解析这些常见错误并提供解决方案。

一、Jasypt版本与算法不匹配
1. 原因及解决方法
在使用Jasypt进行加密时,不同版本的Jasypt默认使用的算法可能不同,Jasypt 3.0以上版本默认使用PBEWITHHMACSHA512ANDAES_256
算法,而3.0以下版本则使用PBEWithMD5AndDES
算法,如果加密和解密时使用的算法不一致,就会导致解密失败。
解决方法:确保加解密时使用的算法一致,可以选择统一使用PBEWITHHMACSHA512ANDAES_256
算法,或者统一使用PBEWithMD5AndDES
算法。
2. 示例代码
- 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();
- SimpleStringPBEConfig config = new SimpleStringPBEConfig();
- config.setPassword(password);
- config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
- 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);
- return encryptor.encrypt(value);
- }
- /**
- * 解密
- * @param password 配置文件中设定的加密盐值
- * @param value 解密密文
- * @return
- */
- public static String decyptPwd(String password, String value) {
- PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
- SimpleStringPBEConfig config = new SimpleStringPBEConfig();
- config.setPassword(password);
- config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
- 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);
- return encryptor.decrypt(value);
- }
- public static void main(String[] args) {
- // 加密
- String encPwd = encyptPwd("jasypt", "root");
- // 解密
- String decPwd = decyptPwd("jasypt", encPwd);
- System.out.println("加密后的密文:" + encPwd);
- System.out.println("解密后的明文:" + decPwd);
- }
- }
二、Boot版本与Jasypt兼容性问题
1. 原因及解决方法
不同版本的Spring Boot对Jasypt的支持情况不同,Jasypt 2.1.x版本对应Spring Boot 2.1.x版本,而Jasypt 3.0.x版本则对应Spring Boot 2.2.x及以上版本,如果不匹配,可能会导致启动报错。

解决方法:确保Spring Boot版本与Jasypt版本兼容,可以通过查阅官方文档或相关资源确认兼容性。
2. 示例配置
- <dependency>
- <groupId>com.github.ulisesbocchio</groupId>
- <artifactId>jasyptspringbootstarter</artifactId>
- <version>3.0.3</version>
- </dependency>
三、密码设置错误导致IllegalArgumentException
1. 原因及解决方法
在使用Jasypt时,如果在application.yml中配置了错误的密码或未正确设置密码,会抛出IllegalArgumentException: Password cannot be set empty
异常,这通常是因为直接复制了别人的密文而没有重新生成自己的密文。
解决方法:确保在application.yml中正确配置了密码,并且密码是通过Jasypt工具生成的,可以使用命令行工具生成密文:

- java cp jasypt1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=salt algorithm=PBEWithMD5AndDES
2. 示例配置
- spring:
- datasource:
- driverclassname: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&useSSL=true&serverTimezone=GMT%2B8
- username: ENC(hEc7NPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3)
- password: ENC(MaF3AwPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3)
在使用Jasypt进行数据库敏感信息加密时,开发者可能会遇到多种报错问题,通过本文的详细解析,可以发现大多数问题源于版本不匹配、算法不一致或配置错误,确保使用正确的版本、统一的算法以及正确的配置,可以有效避免这些问题,建议开发者在实际应用中仔细阅读官方文档,并根据具体需求进行调整,以下是两个常见问题的FAQs:
Q1:为什么使用Jasypt加密后的数据无法解密?
A1: 可能是由于加密和解密时使用的算法不一致,确保在加密和解密时都使用相同的算法,如PBEWITHHMACSHA512ANDAES_256
或PBEWithMD5AndDES
。
Q2:如何解决Spring Boot启动时因密码配置错误导致的IllegalArgumentException?
A2: 确保在application.yml中正确配置了密码,并且密码是通过Jasypt工具生成的,避免直接复制别人的密文,而是使用命令行工具或代码生成自己的密文。