Jasypt加密/解密工具嵌入Spring boot框架的使用教程

一、Jasypt 是什么?

        Jasypt (Java Simplified Encryption) 是一个开源的Java库,专注于简化加解密过程,特别适合在Java应用程序中处理敏感数据,如密码、密钥或任何其他需要保护的信息。Jasypt的目标是提供一个易于使用的API,使得开发者能够轻松地集成加密功能,而不必成为加密专家。以下是Jasypt的一些关键特性及用途:

关键特性:

  • 文本加密与解密:Jasypt允许你加密字符串,并在需要时轻松解密回原始形式。这对于存储如数据库密码、API密钥等敏感信息非常有用。
  • 密码器支持:它支持多种加密算法(如PBEWITHMD5ANDDES、PBEWITHSHA256AND256BITAES-CBC-BC等),并允许自定义加密策略。
  • 配置文件加密:特别适用于Spring框架,Jasypt可以透明地解密在配置文件中加密的属性,比如数据库连接字符串中的密码。
  • 环境感知:可以在不同的环境中(如开发、测试、生产)使用不同的加密策略和密钥,增强安全性。
  • 无状态操作:Jasypt的加密操作不依赖于外部状态,使得它易于部署和使用。
  • 集成友好:提供了与Spring框架的紧密集成,可以通过Spring Boot Starter轻松配置和使用。

用途

  • 安全存储配置信息:在配置文件中加密mysql、redis等数据库密码,防止明文泄露。
  • 应用内加密逻辑:在应用内部对敏感数据进行加解密操作,例如用户密码的存储和验证。
  • 日志安全:在记录包含敏感信息的日志前对其进行加密,确保日志安全。
  • 数据交换安全:在不同系统间交换数据时,对数据进行加密传输,保障数据传输的安全性。

二、使用步骤

环境要求

  • SpringBoot2.0以上
  • Jasypt 3.0.3

2.1 pom.xml文件中引入依赖

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

2.2 在本地application-dev.yml文件中添加Jasypt配置

jasypt:
  encryptor:
    # 盐值
    password: 123
    # 指定加密方式
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
    property:
      # 标识为加密属性的前缀
      prefix: ENC(
      # 标识为加密属性的后缀
      suffix: )

注意:这里是解密配置,加密时的算法和盐值必须与以上配置中的algorithm与password(盐值)一致。

盐值概念:
        在密码学中,盐值(Salt)是一种随机数据,通常用于加强密码的哈希过程,以增加破解的难度。在Jasypt(Java Simplified Encryption)中,盐值(也称为密钥或加密密码)是用于加密和解密过程的一个重要组成部分。它是一个额外的输入,与待加密的数据一起使用,以生成一个特定的加密结果。这样,即使相同的明文数据被加密多次,每次都会因为不同的盐值而得到不同的密文。

2.3 编写JasyptUtil工具类用于加密/解密

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));
    }
}

2.3.1 编写测试类进行测试jasypt加密测试

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加密后的密码)。

2.4 修改application-dev.yml配置文件

修改mysql数据库连接密码:

修改redis数据库连接密码:

通过以上的操作就配置完成啦,进一步提高了系统的安全性。

  • 17
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值