1、简述
Jasypt (Java Simplified Encryption) 是一个简化 Java 应用中加密工作的库。它支持加密和解密操作,易于与 Spring Boot 集成。通过 Jasypt,可以安全地管理敏感信息,比如数据库密码、API 密钥等。
2、核心功能
- 简化的加解密操作:通过易用的 API 提供加密和解密功能。
- 多种算法支持:如 AES、PBE 等。
- 支持属性加密:与 Spring 的 @Value 注解无缝集成,直接解密配置文件中的敏感信息。
- 安全性高:支持盐值(Salt)和迭代计数(Iteration Count)以增强安全性。
3、实践样例
3.1 Maven 依赖
添加以下依赖到你的 pom.xml 文件中:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
3.2 配置应用
- 配置文件
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=ENC(encrypted_password)
- 加密工具
使用 jasypt-spring-boot 提供的 CLI 工具加密密码:
jasypt encrypt input=my_password password=my_secret_key algorithm=PBEWithMD5AndDES
输出结果类似:
ENC(3bf2jN+/NfM45y8OeM7TfQ==)
3.3 动态设置加密器
在 Spring Boot 项目中,可以通过配置动态设置加密器的属性。
application.properties:
jasypt.encryptor.password=my-strong-secret-key
jasypt.encryptor.algorithm=PBEWithHMACSHA512AndAES_256
jasypt.encryptor.key-obtention-iterations=2000
jasypt.encryptor.pool-size=4
jasypt.encryptor.salt-generator-classname=org.jasypt.salt.RandomSaltGenerator
自定义配置类:
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.salt.RandomSaltGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JasyptConfig {
@Bean("jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setPassword("my-strong-secret-key");
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
encryptor.setKeyObtentionIterations(2000);
encryptor.setPoolSize(4);
encryptor.setSaltGenerator(new RandomSaltGenerator());
return encryptor;
}
}
4、加密算法
4.1 使用高级算法进行加密和解密
默认的 PBEWithMD5AndDES 算法安全性不够高,可以使用更安全的 PBEWithHMACSHA512AndAES_256。
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
public class AdvancedJasyptExample {
public static void main(String[] args) {
// 创建加密器
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("my-strong-secret-key"); // 设置密钥
encryptor.setAlgorithm("PBEWithHMACSHA512AndAES_256"); // 设置高级算法
// 加密
String sensitiveData = "SuperSecretPassword123";
String encryptedData = encryptor.encrypt(sensitiveData);
System.out.println("Encrypted Data: " + encryptedData);
// 解密
String decryptedData = encryptor.decrypt(encryptedData);
System.out.println("Decrypted Data: " + decryptedData);
}
}
4.2 使用 Salt 和 Iteration Count 增强加密
Salt(盐值)和 Iteration Count(迭代计数)可以显著提高加密的安全性。
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.salt.RandomSaltGenerator;
public class SaltAndIterationExample {
public static void main(String[] args) {
// 创建加密器
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setPassword("my-strong-secret-key");
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
encryptor.setPoolSize(4); // 线程池大小
// 设置盐值生成器和迭代次数
encryptor.setSaltGenerator(new RandomSaltGenerator());
encryptor.setKeyObtentionIterations(1000); // 增加破解难度
// 加密
String sensitiveData = "ImportantDataToEncrypt";
String encryptedData = encryptor.encrypt(sensitiveData);
System.out.println("Encrypted Data: " + encryptedData);
// 解密
String decryptedData = encryptor.decrypt(encryptedData);
System.out.println("Decrypted Data: " + decryptedData);
}
}
5、应用场景
Jasypt的优势在于其简单易用的API和强大的加密功能。它提供了多种加密器的选择,可以根据具体需求选择适合的加密器。同时,Jasypt还支持敏感数据的加密配置,可以将加密后的敏感数据存储在配置文件中,提高了应用程序的安全性。
Jasypt的应用场景包括但不限于以下几个方面:
- 数据库密码加密:将数据库连接密码加密存储,提高数据库的安全性。
- API密钥保护:将API密钥加密存储,防止密钥泄露导致的安全风险。
- 用户密码加密:将用户密码加密存储,保护用户的隐私数据。
- 配置文件加密:将应用程序的配置文件中的敏感数据加密存储,提高应用程序的安全性。
6、总结
Jasypt 是一个强大且易用的加密工具,特别适合 Java 应用中敏感信息的加密需求。在实际项目中,通过 Jasypt 提供的功能,可以在不改动大量代码的情况下,提高系统的安全性。
评论区