21、Jasypt-SpringBoot配置文件信息加密

千里之行,始于足下

 


正文

Jasypt(Java Simplified Encryption)是一个轻量级的Java加密库,旨在简化加密操作,使Java开发者能够在应用程序中轻松地实现数据加密和解密。它支持多种常见的加密算法,并提供了易于使用的 API 和加密容器,帮助开发者保护敏感信息(如密码、密钥、API 密钥、数据库密码等)。

最常见的加密算法包括:

1、PBEWithMD5AndDES:基于密码短语的加密(使用 MD5 和 DES 算法)。

2、AES (Advanced Encryption Standard):一种对称加密算法,通常用于较强的加密需求。

3、RSA:非对称加密算法,用于公共密钥加密。

4、PBKDF2:一种基于密码的密钥派生函数,用于增强密码强度。

 

回到顶部

Jasypt-SpringBoot的基本用法:

回到顶部

1、POM依赖:

<!-- 加解密依赖-->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
回到顶部

2、YML配置:

复制代码
# 加密配置
jasypt:
  encryptor:
    # 指定加密密钥,生产环境建议放到启动参数
    password: your-secret
    # 指定解密算法,需要和加密时使用的算法一致
    algorithm: PBEWithMD5AndDES
    # 指定initialization vector类型
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
复制代码
回到顶部

3、自定义加解密工具类:

复制代码
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.util.ObjectUtils;

/**
 * 加解密工具类
 */
public class EncrypDecryptUtil {

    /**
     * 加密
     *
     * @param password 加密时使用的密码
     * @param value    需要加密的值
     * @return
     */
    public static String encypt(String password, String value) {
        if(ObjectUtils.isEmpty(value)){
            return null;
        }
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword(password);
        return encryptor.encrypt(value);
    }

    /**
     * 解密
     *
     * @param password 解密时使用的密码
     * @param value    需要解密的值
     * @return
     */
    public static String decypt(String password, String value) {
        if(ObjectUtils.isEmpty(value)){
            return null;
        }
        StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
        decryptor.setPassword(password);
        return decryptor.decrypt(value);
    }

    public static void main(String[] args) {
        // "your-secret": YML中配置
        //加密
        System.out.println(EncrypDecryptUtil.encypt("your-secret", "root"));
        System.out.println(EncrypDecryptUtil.encypt("your-secret", "root"));
        //解密
        System.out.println(EncrypDecryptUtil.decypt("your-secret", "7SZVFKSF09DDdmLwM8pU9dGKw=="));

    }

}
复制代码
回到顶部

4、相关使用:

ENC(密文):用于标记加密数据,其中括号中的部分是加密后的内容

 

posted on   爱文(Iven)  阅读(189)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示

目录导航