【springboot】jasypt加密

article/2025/10/9 0:04:45

参考:

  • https://blog.csdn.net/enthan809882/article/details/105529349
  • https://amore.blog.csdn.net/article/details/111468221
  • https://blog.csdn.net/weixin_49076592/article/details/119797675

Jasypt

Jasypt是加密库,支持密码、Digest认证、文本、对象加密。

密码加密复合RFC2307标准。http://www.jasypt.org/download.html

spring项目参考:https://blog.csdn.net/gdfsbingfeng/article/details/16886805

仓库地址:https://github.com/ulisesbocchio/jasypt-spring-boot

使用过程也可查看仓库README.md:https://github.com/ulisesbocchio/jasypt-spring-boot

maven官网地址:https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter

springboot使用jasypt

配置信息只有 jasypt.encryptor.password 是必须的,配置项有:

  • 旧版本(图片引用来源)

在这里插入图片描述

  • 新版本(图片引用来源)

在这里插入图片描述

需要注意版本对应

jasypt-spring-boot-starter依赖的 spring-boot-starter
2.1.02.0.3.RELEASE 2.2.6.RELEASE
2.0.02.0.0.RELEASE 2.2.6.RELEASE
1.181.5.10.RELEASE 2.2.6.RELEASE
1.121.5.1.RELEASE 2.2.6.RELEASE

需要注意加解密的类型一致,如:

2.1.2版本默认加密方式为:PBEWithMD5AndDES
3.0.3版本默认加密方式为:PBEWITHHMACSHA512ANDAES_256
当引入3.0.3依赖,却没有添加相关jasypt加解密配置,而密文通过【PBEWithMD5AndDES】来加密,启动会报错。
需要切换为【PBEWITHHMACSHA512ANDAES_256】方式进行。

简单使用例子

1、添加测试程序

  • 添加依赖
<!-- 方式1:引入 jasypt-spring-boot-starter -->
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>2.1.2</version>
</dependency><!-- 方式2:引入 jasypt-spring-boot  需在启动类添加@EnableEncryptableProperties -->
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot</artifactId><version>2.0.0</version>
</dependency>
  • 添加springboot接口
@RestController
public class TestController {@Value("${test.password:testpassword}")private String jasyptTestPassword;@RequestMapping(value = "/testJasypt", method = RequestMethod.GET)public Object testJasyptPassword(){return "get password:" + jasyptTestPassword;}
}
  • 配置文件添加加密后的配置
test:password: ENC(获取到的密文)# 如果密文加盐,需要配置盐值
jasypt:encryptor:password: 盐值

2、获取密文

2-1、通过java程序获取密文

// -----默认加解密
@Autowired
StringEncryptor stringEncryptor;@Test
public void encryptPwd() {String passwordEn = stringEncryptor.encrypt("root");String passwordDe = stringEncryptor.decrypt(passwordEn);System.out.println("password密文:" + passwordEn);System.out.println("password明文:" + passwordDe);
}// -----加盐加解密
@Test
public void test() {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();encryptor.setPassword("hello"); //设置加密盐值String passwordEn = encryptor.encrypt("root");String passwordDe = encryptor.decrypt(passwordEn);System.out.println("password密文:" + passwordEn);System.out.println("password明文:" + passwordDe);
}// -----加解密工具类
public class JasypUtil {private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";/*** @Description: Jasyp加密(PBEWithMD5AndDES)* @Author:      Rambo* @CreateDate:  2020/7/13 10:24* @UpdateUser:  Rambo* @UpdateDate:  2020/7/13 10:24* @param		 plainText      待加密的原文* @param		 factor         加密秘钥* @return       java.lang.String* @Version:     1.0.0*/public static String encryptWithMD5(String plainText, String factor) {// 1. 创建加解密工具实例StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();// 2. 加解密配置EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();config.setAlgorithm(PBEWITHMD5ANDDES);config.setPassword(factor);encryptor.setConfig(config);// 3. 加密return encryptor.encrypt(plainText);}/*** @Description: Jaspy解密(PBEWithMD5AndDES)* @Author:      Rambo* @CreateDate:  2020/7/13 10:28* @UpdateUser:  Rambo* @UpdateDate:  2020/7/13 10:28* @param		 encryptedText      待解密密文* @param		 factor             解密秘钥* @return       java.lang.String* @Version:     1.0.0*/public static String decryptWithMD5(String encryptedText, String factor) {// 1. 创建加解密工具实例StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();// 2. 加解密配置EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();config.setAlgorithm(PBEWITHMD5ANDDES);config.setPassword(factor);encryptor.setConfig(config);// 3. 解密return encryptor.decrypt(encryptedText);}/*** @Description: Jasyp 加密(PBEWITHHMACSHA512ANDAES_256)* @Author:      Rambo* @CreateDate:  2020/7/25 14:34* @UpdateUser:  Rambo* @UpdateDate:  2020/7/25 14:34* @param		 plainText  待加密的原文* @param		 factor     加密秘钥* @return       java.lang.String* @Version:     1.0.0*/public static String encryptWithSHA512(String plainText, String factor) {// 1. 创建加解密工具实例PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();// 2. 加解密配置SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword(factor);config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置config.setKeyObtentionIterations( "1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);// 3. 加密return encryptor.encrypt(plainText);}/*** @Description: Jaspy解密(PBEWITHHMACSHA512ANDAES_256)* @Author:      Rambo* @CreateDate:  2020/7/25 14:40* @UpdateUser:  Rambo* @UpdateDate:  2020/7/25 14:40* @param		 encryptedText  待解密密文* @param		 factor         解密秘钥* @return       java.lang.String* @Version:     1.0.0*/public static String decryptWithSHA512(String encryptedText, String factor) {// 1. 创建加解密工具实例PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();// 2. 加解密配置SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword(factor);config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置config.setKeyObtentionIterations( "1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);// 3. 解密return encryptor.decrypt(encryptedText);}
}

2-2、通过jasypt中jar包程序获取密文

  • 添加依赖,下载好jar包到本地maven仓库后,cmd进入jasypt.jar包所在的目录
如个人本地目录:D:\Environmental\RepMaven\org\jasypt\jasypt\1.9.3
  • 加密命令,参数说明:
    • input:需要加密的字段
    • password:加密盐值,用来进行加密
    • algorithm:加密方式,默认不写也行
java -cp jasypt-1.9.3.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=hello algorithm=PBEWithMD5AndDES# 输出
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: root
password: hello----OUTPUT----------------------
aCEx6r9g2lBuGF8w/XU8wQ==
  • 解密命令
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="aCEx6r9g2lBuGF8w/XU8wQ==" password=hello algorithm=PBEWithMD5AndDES#输出
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: aCEx6r9g2lBuGF8w/XU8wQ==
password: hello----OUTPUT----------------------
root

3、使用密文:ENC(密文)

  • 如数据源密码加密
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mytest?characterEncoding=UTF-8&&serverTimezone=GMTusername: rootpassword: ENC(AxfLzLN0ayIYc7dGypds0g==)  # 明文=root
  • 测试程序中密码配置加密
test:password: ENC(aCEx6r9g2lBuGF8w/XU8wQ==) # 未加盐:bnwAMepYNbDeCkENg+cerQ==   明文=root# 如果密文加盐,需要配置盐值
jasypt:encryptor:password: hello

4、访问测试接口

#访问接口
http://localhost:8080/testJasypt#结果
get password:root

异常问题

jce权限问题

org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. 
A possible cause is you are using strong encryption algorithms and you have not installed the Java 
Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine# 解决方案:
查看是否是加解密类型不一致导致。
或者下载:https://www.oracle.com/java/technologies/javase-jce8-downloads.html
下载压缩包解压,将local_policy.jar和US_export_policy.jar替换Java\jdk1.8.0_77\jre\lib\security\路径下的jar包

yml中带有@引起的问题

yml中@是特殊字符, 含有@左右需要加单引号。
jasypt 自动加密整个文件的时候,会将单引号也当做密码的一部分,这样得到的密文肯定是错的。#解决方案:
直接将密码生成,然后再复制过去,不要带双引号。

关于盐值配置设置

关于盐值配置方式。注意:关于盐值明文存放到配置文件,一样存在安全风险。

1、配置文件/配置中心设置

jasypt:encryptor:password: hello

2、系统环境变量设置

  • 在系统的环境变量中进行配置。

在这里插入图片描述

  • 配置文件中获取
jasypt:encryptor:password: ${ENCRYPT:hello}  # 系统环境找不到,默认使用hello

3、启动参数设置

  • 在启动配置中添加参数

在这里插入图片描述

在idea启动设置,本地可以正常启动,但是打包到服务器部署需要在idea中Mave打包和部署添加相关参数:

  • 添加maven打包参数
clean package -Djasypt.encryptor.password=hello

在这里插入图片描述
在这里插入图片描述

  • 启动时添加参数
java -jar -Djasypt.encryptor.password=123456 springboot-jasypt-test.jar

4、服务器配置文件设置

配置到服务器某文件中,启动时加载文件获取。

其他

  • 不自定义加密类的话,默认算法为 PBEWithMD5AndDES

  • 多次生成,每次生成的密码不一样。不同的密码序列,解密却可以一样。

  • ENC前缀可改变,即自定义格式:需要添加配置

jasypt:encryptor:property:prefix: "TEST["suffix: "]"

http://chatgpt.dhexx.cn/article/CAL8S9VT.shtml

相关文章

jasypt-spring-boot 加密敏感信息

文章目录 一、简介二、导入依赖三、加密字段工具类四、application.yaml 配置五、启动类测试 一、简介 在后端开发中有很多敏感信息&#xff0c;比如数据库用户名密码&#xff0c;第三方 Apikey&#xff0c;云服务商的 secretKey 等、如果不希望用明文在 application.yml 配置…

jasypt-spring-boot敏感信息加密解密利器使用指南

1. 简介 Springboot整合Jasypt&#xff0c;实现配置信息的安全&#xff0c;如数据库连接.账号和密码.接口凭证信息等。 Jasypt可以为Springboot加密的信息很多&#xff0c;主要有&#xff1a; System Property 系统变量 Envirnment Property 环境变量 Command Line argument 命…

Spring Boot项目使用 jasypt 加密组件进行加密(例如:数据库、服务的Key、等等进行加密)

&#x1f353; 简介&#xff1a;java系列技术分享(&#x1f449;持续更新中…&#x1f525;) &#x1f353; 初衷:一起学习、一起进步、坚持不懈 &#x1f353; 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正&#x1f64f; &#x1f353; 希望这篇文章对你有所帮助,欢…

jasypt中的加密与解密

jasypt由于其使用的是PBEWithMD5AndDES加密方式&#xff0c;所以每次加密出来的结果都不一样&#xff0c;所以很适合对数据进行加密 没什么好说的了&#xff0c;直接看demo。 基本操作 添加依赖 <dependency><groupId>com.github.ulisesbocchio</groupId>&…

jasypt的基本原理

文章目录 一、jasypt的启动类加载二、 jasypt中Encryptor、Detector和Resolver加载三、jasypt中EnableEncryptablePropertiesBeanFactoryPostProcesso四、proxy代理方式增强propertySources五、wrapper包装类方式增强propertySources六、resolver中属性值解密 作者&#xff1a;…

Jasypt 开源加密库使用教程

目录 Jasypt 加密概述 Jasypt 快速使用 StringEncryptor 加解密 Jasypt 配置详解 Jasypt 加密概述 1、Jasypt Spring Boot 为 spring boot 应用程序中的属性源提供加密支持&#xff0c;出于安全考虑&#xff0c;Spring boot 配置文件中的敏感信息通常需要对它进行加密/脱敏…

使用jasypt 进行配置文件加密

项目中application.yml 配置文件的各种链接的username&#xff0c;password的值都是明文的&#xff0c;其实存在一定的安全隐患&#xff0c;这是可以使用jasypt 的方式进行明文加密, 需要注意的是 盐值和密文分开保存 参考学习-1 参考学习-2 下面实现最简单的配置 项目是sprin…

Jasypt实现数据加解密(脱敏)

场景一&#xff1a;对配置文件中的裸露的密码进行加密 1、添加依赖 <dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.4</version></dependency> …

加密组件Jasypt学习、实战及踩坑记录

概述 最近入职新公司&#xff0c;因几乎全部项目都使用到jasypt&#xff0c;故而初步学习记录下本文&#xff08;持续更新&#xff09;。 官网及GitHub给出的简介&#xff1a;使用简单&#xff0c;性能好&#xff0c;特性features非常丰富&#xff1b;支持 另&#xff0c;有个…

Jasypt加解密

Jasypt加密 Jasypt加密引言介绍整合SpringBoot引入依赖编写配置加密和解密通过Java程序获取密文和解密通过jasypt中jar包程序获取密文使用密文&#xff1a;ENC(密文) 异常问题JCE权限问题yml中带有引起的问题 关于盐值&#xff08;密钥&#xff09;配置设置其他 Jasypt加密 引…

微服务SpringBoot整合Jasypt加密工具

文章目录 一、Jasypt介绍二、Jasypt手动使用2.1 密码加密场景2.2 文本加密场景2.3 数值加密场景2.4 二进制数据加密场景 三、Jasypt整合SpringBoot四、生成环境启动无、参考文档 一、Jasypt介绍 Jasypt是Java加密工具包&#xff0c;能支持对密码的哈希加密&#xff0c;对文本和…

Springboot之Jasypt配置文件加密/解密

Jasypt配置文件加密/机密 一、Jasypt介绍二、Springboot整合Jasypt2.1 环境配置2.2 添加依赖2.3 添加Jasypt配置2.4 编写加/解密工具类2.5 修改配置文件2.5 如何进一步防止密码泄露2.5.1 自定义加密器2.5.3 加密盐值通过环境变量指定 文章系列 【一、Springboot之Jasypt配置文…

java中byte与int的转换原理

前些天遇到一个问题&#xff0c;byte[0] "A3"&#xff08;十六进制表示&#xff09; 但是在debug时显示的是 -93 &#xff0c;而如果直接赋值给int的变量也是-93.当然大部分人都知道这是不能直接赋值的&#xff0c;需要以下面的这种方式将byte转换成int&#xff1a;…

Java byte 转化为 String

1、Java 中 byte 转化为 String&#xff0c;代码如下 package nice.com.mian;import java.io.UnsupportedEncodingException;public class StringMain {public static void main(String[] args) throws Exception {byte[] bb {97,99,105,51,55};String str new String(bb, &…

Java byte[] 转 String的简单转换

简单的将byte[] 还原成了 String 值 [TOC] 代码&#xff1a; public class Test0719 {public static void main(String[] args) {String text "test";byte[] textBytes text.getBytes();String content byteToString(textBytes);System.out.println(textBytes &q…

java:int强制类型转换成byte

注&#xff1a;非常感谢评论的最佳戏子大佬指出了我的不足&#xff0c;已经进行修改 int强制类型转换成byte 一、基础知识二、int->byte方法一方法二 一、基础知识 int 在java中是32位&#xff0c; byte是8位 原码&#xff1a;就是二进制码&#xff0c;最高位为符号位&…

C#初级编程

Unity学习笔记——C#初级编程 一、Awake 和 Start二、Update 和 FixedUpdate三、启用和禁用组件四、SetActive、Active Self、Active in Hierarchy五、位置和旋转六、transform.lookAt(Transform target)七、线性插值八、Destroy九、GetButton 和 GetKey十、GetAxis十一、GetCo…

零基础怎样自学编程?新手如何学习编程?编程学习入门指南

对于编程&#xff0c;很多新手的第一感觉可能就是&#xff1a; 高深&#xff0c;难学。 学好编程&#xff0c;有的时候&#xff0c;可以把一些需要我们重复劳动的工作&#xff0c;自动化批量处理&#xff0c;为我们节省很多时间和精力。 对于一些学得比较深入的朋友来说&…

新手学计算机编程怎么入门 从哪学起

近些年有一种职业发展很快、人才需求量大、工资高&#xff0c;那就是程序员。他们的基本工作就是电脑编程&#xff0c;开发者各种各样的软件、APP&#xff0c;被很多人膜拜。如果你想成为一名程序员&#xff0c;有必要了解一下这些基本的电脑编程入门教程。 1计算机编程怎么入门…

代码编程教学入门

前言 代码编程是我们在现代能获得的最宝贵的技能之一&#xff0c;学会写代码不仅让我们在职业前景上更得心应手&#xff0c;而且还能让我们的大脑保持活跃和创造性&#xff0c;甚至我们还有机会创造出一些 awesome 的东西出来。 如果我们才刚刚开始&#xff08;或准备开始&am…