赞
踩
Jasypt (Java Simplified Encryption) 是一个开源的Java库,专注于简化加解密过程,特别适合在Java应用程序中处理敏感数据,如密码、密钥或任何其他需要保护的信息。Jasypt的目标是提供一个易于使用的API,使得开发者能够轻松地集成加密功能,而不必成为加密专家。以下是Jasypt的一些关键特性及用途:
关键特性:
用途
- <dependency>
- <groupId>com.github.ulisesbocchio</groupId>
- <artifactId>jasypt-spring-boot-starter</artifactId>
- <version>3.0.3</version>
- </dependency>
- jasypt:
- encryptor:
- # 盐值
- password: 123
- # 指定加密方式
- algorithm: PBEWithMD5AndDES
- iv-generator-classname: org.jasypt.iv.NoIvGenerator
- property:
- # 标识为加密属性的前缀
- prefix: ENC(
- # 标识为加密属性的后缀
- suffix: )
盐值概念:
在密码学中,盐值(Salt)是一种随机数据,通常用于加强密码的哈希过程,以增加破解的难度。在Jasypt(Java Simplified Encryption)中,盐值(也称为密钥或加密密码)是用于加密和解密过程的一个重要组成部分。它是一个额外的输入,与待加密的数据一起使用,以生成一个特定的加密结果。这样,即使相同的明文数据被加密多次,每次都会因为不同的盐值而得到不同的密文。
- public class JasyptUtil {
-
- /**
- * PBE 算法
- */
- public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
- public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
- public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
- public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";
-
- private JasyptUtil() {
- }
-
- /**
- * Jasypt 加密
- *
- * @param encryptedStr 加密字符串
- * @param password 盐值
- * @return
- */
- public static String encrypt(String encryptedStr, String password) {
- return encrypt(encryptedStr, PBE_ALGORITHMS_MD5_DES, password);
- }
-
- /**
- * Jasypt 加密
- *
- * @param encryptedStr 加密字符串
- * @param algorithm 加密算法
- * PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
- * @param password 盐值
- * @return
- */
- public static String encrypt(String encryptedStr, String algorithm, String password) {
- // StandardPBEStringEncryptor、StandardPBEBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
- StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
- EnvironmentPBEConfig config = new EnvironmentPBEConfig();
-
- // 指定加密算法
- config.setAlgorithm(algorithm);
- // 加密盐值
- config.setPassword(password);
- //config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
- encryptor.setConfig(config);
-
- // 加密
- return encryptor.encrypt(encryptedStr);
- }
-
- /**
- * Jasypt 解密
- *
- * @param decryptStr 解密字符串
- * @param password 盐值
- * @return
- */
- public static String decrypt(String decryptStr, String password) {
- return decrypt(decryptStr, PBE_ALGORITHMS_MD5_DES, password);
- }
-
- /**
- * Jasypt 解密
- *
- * @param decryptStr 解密字符串
- * @param algorithm 指定解密算法:解密算法要与加密算法一一对应
- * PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
- * @param password 盐值
- * @return
- */
- public static String decrypt(String decryptStr, String algorithm, String password) {
- // StandardPBEStringEncryptor、StandardPBEBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
- StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
- EnvironmentPBEConfig config = new EnvironmentPBEConfig();
-
- // 指定解密算法:解密算法要与加密算法一一对应
- config.setAlgorithm(algorithm);
- // 加密秘钥
- config.setPassword(password);
- //config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
- encryptor.setConfig(config);
-
- // 解密
- return encryptor.decrypt(decryptStr);
- }
-
- public static void main(String[] args) {
- String encryptedStr = "I am the string to be encrypted";
- String algorithm = PBE_ALGORITHMS_SHA1_RC2_40;
- String password = "salt";
-
- String str = JasyptUtil.encrypt(encryptedStr, algorithm, password);
- System.out.println("加密后的字符串:" + str);
- System.out.println("解密后的字符串:" + JasyptUtil.decrypt(str, algorithm, password));
- }
- }

- package com.agileboot.admin.jasypt;
- import com.agileboot.common.utils.jasypt.JasyptUtil;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- public class jasyptTest {
-
- /**
- * PBE 算法
- */
- public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
- public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
- public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
- public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";
-
-
- @Test
- public void TsetJasypt() {
- String encryptedStr = "root";
- String algorithm = PBE_ALGORITHMS_MD5_DES;
- String password = "salt";
- String str = JasyptUtil.encrypt(encryptedStr, algorithm, password);
- System.out.println("加密后的字符串:" + str);
- System.out.println("解密后的字符串:" + JasyptUtil.decrypt(str, algorithm, password));
- }
-
-
- }

2.3.2 启动类加上@EnableEncryptableProperties这个注解,用于自动解密
运行结果如下图:
可以在以上代码中设置自己需要加密的明文、盐值、算法,然后将加密后的密码填入application-dev.yml文件中配置mysql、redis等数据库的密码填写位置。注意填写方式为ENC(经过jasypt加密后的密码)。
修改mysql数据库连接密码:
修改redis数据库连接密码:
通过以上的操作就配置完成啦,进一步提高了系统的安全性。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。