jasypt加密

解决问题:

 

chmod 777 jasypt-1.5/bin/encrypt.sh

 

jasypt-1.5/bin/encrypt.sh input="password" password="key"

 

passeord:数据库访问密码

key:密钥

 

 

显示如下:

 

----ENVIRONMENT-----------------

Runtime: Sun Microsystems Inc. Java HotSpot(TM) Client VM 11.2-b01

 

----ARGUMENTS-------------------

input: password
password: key

 

----OUTPUT----------------------

 

fgasgsadfgasdfgasdfasdfsadfasfasf(加密数据)

 

 

打开数据库访问配置,把数据写在ENC函数内:

 

password=ENC(加密数据)

 

 

 

 

### 在 Spring Boot 中使用 Jasypt 进行数据加密 Jasypt 是一个用于 Java 应用程序的加密库,旨在简化加密操作并提升安全性。它支持多种加密算法,例如 PBE(基于密码的加密),并引入了盐值(Salt)和 IV(初始化向量)来增强加密强度,从而有效防止常见的攻击方式,例如彩虹表攻击[^2]。在 Spring Boot 项目中,Jasypt 可以轻松整合到配置文件(如 `application.properties` 或 `application.yml`)中,用于加密敏感信息,例如数据库密码或 API 密钥[^3]。 #### 1. 添加 Jasypt 依赖 首先,在 `pom.xml` 文件中添加 **jasypt-spring-boot-starter** 的依赖,以启用 Jasypt 的功能: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.5</version> </dependency> ``` #### 2. 配置加密密钥 在 `application.properties` 或 `application.yml` 文件中,配置用于加密的密钥。密钥是加密和解密的核心,必须妥善保管。 ##### `application.properties` 示例: ```properties jasypt.encryptor.password=your-secret-key jasypt.encryptor.algorithm=PBEWithHMACSHA256AndAES_128 jasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator ``` ##### `application.yml` 示例: ```yaml jasypt: encryptor: password: your-secret-key algorithm: PBEWithHMACSHA256AndAES_128 iv-generator-classname: org.jasypt.iv.RandomIvGenerator ``` #### 3. 使用 Jasypt 加密数据 Jasypt 提供了 CLI 工具,可以直接生成加密字符串。例如,要加密明文数据 `"my-secret-data"`,可以使用以下命令: ```bash java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="my-secret-data" password=your-secret-key algorithm=PBEWithHMACSHA256AndAES_128 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator ``` 运行后,CLI 工具将输出加密后的字符串,例如 `ENC(encrypted-string)`。这个字符串可以直接用于配置文件中[^4]。 #### 4. 在配置文件中使用加密值 将生成的加密字符串插入到配置文件中,前缀为 `ENC()`。例如: ```properties # application.properties 示例 app.secret.key=ENC(encrypted-string) ``` ```yaml # application.yml 示例 app: secret: key: ENC(encrypted-string) ``` Spring Boot 会在启动时自动解密这些值。 #### 5. 在代码中使用加密值 在 Spring Boot 应用程序中,可以通过 `@Value` 注解直接注入解密后的值: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class SecretConfig { @Value("${app.secret.key}") private String secretKey; public String getSecretKey() { return secretKey; } } ``` #### 6. 自定义加密解密逻辑(可选) 如果需要在代码中直接调用 Jasypt 进行加密或解密,可以使用 `StandardPBEStringEncryptor`: ```java import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; public class JasyptUtil { private static final String PASSWORD = "your-secret-key"; public static String encrypt(String data) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(PASSWORD); encryptor.setAlgorithm("PBEWithHMACSHA256AndAES_128"); return encryptor.encrypt(data); } public static String decrypt(String encryptedData) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(PASSWORD); encryptor.setAlgorithm("PBEWithHMACSHA256AndAES_128"); return encryptor.decrypt(encryptedData); } public static void main(String[] args) { String original = "Hello, World!"; String encrypted = encrypt(original); String decrypted = decrypt(encrypted); System.out.println("Original: " + original); System.out.println("Encrypted: " + encrypted); System.out.println("Decrypted: " + decrypted); } } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值